Question Retrieve mutliple records that will show in a listbox

trialer

Well-known member
Joined
Oct 4, 2010
Messages
64
Programming Experience
1-3
here's my
VB.NET:
    Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
        Dim cmd As New SqlCommand
        Dim da As New SqlDataAdapter
        Dim dt As New DataTable
        Dim s As New BindingSource
        Connect()
        cmd.Connection = con
        cmd.CommandText = ("SELECT Reg_No,CustomerID,Inst_Date,Package,Account_Stat,Title,Name,Address FROM table_subsprofile ORDER BY CustomerID ASC")
        da.SelectCommand = cmd
        da.Fill(dt)
        s.DataSource = dt
        ListBox1.DataSource = s
        Disconnect()
    End Sub

error:
it will show this in listbox1
System.DataRowView

any one knows to shows records in a listbox,,.
 
The problem is that you're not telling the control what data from each item to display. If you don't specify then it just calls ToString on each item. In your case, each item is a DataRowView so you see the result of their ToString methods, i.e. the type name. If you want to see a specific property or column from the items in the bound list then you have to assign the name of that property or column to the DisplayMember property. Generally, you'll also assign the name of the key property or column to the ValueMember, e.g.
VB.NET:
myListBox.DisplayMember = "Description"
myListBox.ValueMember = "ID"
myListBox.DataSource = myDataTable
The user will then see the contents of the Description column. When they make a selection, you can get the corresponding ID value from the SelectedValue property.
 
By the way, if you're Connect and Disconnect methods are opening and closing the connection then they are unnecessary in this context. The Fill and Update methods of a DataAdapter will automatically open and close the connection if required.
 
Back
Top