Saturday, March 24, 2012

Looping and Binding to DataList

I can't seem to find an answer to this one so I thought I would try here. I am trying to bind to a datalist on the fly with the results from a database query with variables. I keep looping to get the different variables from an array to add to the SQL statement. But, I want to bind the result of the first query to the datalist, then the next query, and then the next. Right now, I can only see the final result of my query listed. I am including the code. This is very important for me since I want to finish up a project and this is the last piece of the puzzle.

Dim myArray(x) As string
myArray(0) = Trim(mid(strID,1,2))
myArray(1) = Trim(mid(strID,3,2))
myArray(2) = Trim(mid(strID,5,2))

For CurrentNum = 0 to 2
stringSQL2 = "Select id,names from products where id='" & myArray(CurrentNum) & "'"
response.write (stringSQL2 & "<br>")
Conn = New SQLConnection(strConn)
Dim Cmd2 As New SQLCommand(stringSQL2,Conn)
Conn.Open
Rdr2 = Cmd2.ExecuteReader()
'While Rdr2.Read()
'Response.Write(Rdr2.Item("names") & "<br>")
'End While
myproducts.datasource = Rdr2
myproducts.databind()
Next

The datalist is as follows -

<asp:DataList id="myproducts" runat="server">
<itemtemplate>
<%# DataBinder.Eval ( Container.DataItem, "names" ) %>
</itemtemplate>
</asp:DataList
Thank you for any input
HBdatasources are not cummulative.
when you set a datasource and databind, the object clears what data it had and starts the binding process with a clean slate.

this means that you need to aggregate your data into a single list and then bind once.

0 comments:

Post a Comment