Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding items to Listbox from Database

Status
Not open for further replies.

dvannoy

MIS
May 4, 2001
2,765
US
I'm trying to loop through a select statement and put the results int oa listbox within a grid.

see below. I cannot get the listbox to populate.

Thanks in advance

Select Case e.Row.RowType
Case DataControlRowType.DataRow
myListBox.BackColor = Drawing.Color.Aqua

Dim sql As String = "select Records from Table WHERE week_number = '" & l1.Text & "'"
Dim connection As New SqlConnection(strcn1)

sCommand = New SqlCommand(sql, connection)
Dim dgDS As New DataSet


Dim dgDA As New SqlDataAdapter(sCommand)

dgDA.Fill(dgDS, "Table")

For Each r As DataRow In dgDS.Tables(0).Rows
myListBox.Items.Add(r("FieldName").ToString())
Next

myListBox.Items.Clear()
myListBox.DataSource = dgDS.Tables("Table")
myListBox.DataBind()

End Select

 
Why are you looping through the rows? Just bind to your dataset table.
Code:
myListBox.datasource = dgDS.tables(0)
myListBox.DataTextField = "yourcolumnname"
myListBox.DataValueField = "yourcolumnname"
mylistbox.databind()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top