Click here to Skip to main content
16,013,338 members
Please Sign up or sign in to vote.
1.20/5 (2 votes)
See more:
C#
Public Class customer
       Private name As String
       Private age As Integer
       Private Address As String
       Private salary As Double
       Public Sub New(ByVal cname As String, ByVal cage As Integer, ByVal caddress As String, ByVal sal As Double)
           name = cname
           age = cage
           Address = caddress
           salary = sal
       End Sub

   End Class

 Protected Sub btnFetch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnFetch.Click
       
        Dim lst As New List(Of customer)
        Dim cmd As New SqlCommand("customerData", con)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@id", TextBox1.Text)
        Dim reader As SqlDataReader
        con.Open()
        Try
            reader = cmd.ExecuteReader()
        Catch ex As SqlClient.SqlException
            If InStr(ex.Message, "ID DOES NOT EXISTS", CompareMethod.Text) Then
                MsgBox("ID DOES NOT EXISTS")
            End If
        End Try
        While reader.Read
            lst.Add(New customer(reader("name"), reader("Age"), reader("Address"), reader("Salary")))
        End While
       //here i want to display the values from lst to listbox1
        ' ListBox1.DataSource = lst
        'For Each item As String In ListBox1.Items
        '    ListBox1.Items.Add(item)
        'Next
        con.Close()
Posted
Updated 29-Oct-14 3:22am
v2

1 solution

The ListBox will not bind data for all the columns it needs only two property values to set the DisplayText and the ValueField into the list box.

So let say, you are having Name and ID as those two values i am adding here an example for your help only :-

C#
ListBox1.DataSource = lst;
                ListBox1.DataTextField = "Name";
                ListBox1.DataValueField = "Id";
                ListBox1.DataBind();



NOTE : If you want multiple columns to be displayed then you can use GridView asp.net control for the purpose.

Hope this will definitely of help to you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900