Sorry I got really busy at work and wasn't able to check back in. Is
this still an issue for you? If not, everyone just ignore me.
Given the code example you have below, you want to pass the last
orderid retrieved to a subroutine when the loop has completed. So just
pull the last orderid from the commandargument of lnkCount. The last
time through the loop you will have the same value as you have in your
session variable in the code below. This way you do not have to use a
session variable. You could also create a local variable and assign
the value as you iterate the records. That may be a little cleaner and
easier to understand if someone has to come back and look at this
routine later.
wrote:
> Gozirra, the main reason why I want to get the OrderID of each order
> outside the While(sqlReader.Read) loop is because I want to pass the
> OrderID to another sub named 'GetAddress' which expects 2 parameters -
> the UserID & the OrderID. Depending upon the OrderID (& of course, the
> UserID), the 'GetAddress' sub will retrieve the billing address &
> shipping address pertaining to that OrderID. So even if I create the
> dynamic links inside the While(sqlReader.Read) loop as you have
> suggested, I still can't access the OrderID outside the While loop. In
> order to access the OrderID outside the While loop, I will have to use
> Session variables something like this:
>
> Sub Page_Init(....)
> iUserID = Request.Cookies("UserID").Value
>
> While(sqlReader.Read)
> If (sqlReader.GetValue(0) > 1) Then
> iTotalOrders = sqlReader.GetValue(0)
> 'get the OrderID in Session variable
> Session("OrderID") = sqlReader.GetValue(1)
> lnkCount = New LinkButton
> lnkCount.ID = "lnkCount" & iCount
> lnkCount.Text = iCount.ToString
> lnkCount.CommandName = iCount
> lnkCount.CommandArgument = Session("OrderID")
> AddHandler lnkCount.Command, AddressOf CommandCount
> pnlLinks.Controls.Add(lnkCount)
> iCount = iCount + 1
> Session("TotalOrders") = iTotalOrders
> Else
> pnlLinks.Visible = False
> End If
> End While
>
> GetAddress(iUserID, CInt(Session("OrderID")))
> End Sub
>
> This does serve my purpose & gets the correct billing & shipping
> address corresponding to the UserID & OrderID but I did like to avoid
> using Session variables.
>
> Can you suggest some way by which I can avoid using Session variables
> to store the OrderID?
>
> Gozirra wrote:
> > Why build the list outside of the while loop then? Try something like
> > this. Since I do not know exactly what data is coming back, its kind
> > of hard to determine exactly what all of the possibilities are. But
> > hopefully it'll give you an idea if you haven't already gotten
> > everything fixed.
> >
> > While (sqlReader.Read)
> > If (sqlReader.GetValue(0) > 1) Then
> > iTotalOrder = sqlReader.GetValue(0)
> >
> > lnkDynamic = new LinkButton()
> > lnkDynamic.EnableViewState = true
> > lnkDynamic.ID = "lnkDynamic_" + <identifier> 'Identifier
> > can be your order# or whatever your want
> > lnkDynamic.Text = "SomeText"
> > AddHandler lnkDynamic.Command, AddressOf lnkDynamic_Command
> >
> > lnkDynamic.CommandName = "AnyTextYouWant"
> > 'sqlReader.GetValue(1) perhaps
> > lnkDynamic.Visible = false
> > Me.Controls.Add(lnkDynamic)
> > Else
> > pnlLinks.Visible = False
> > End If
> > End While