locked
need help transfering data table to listbox. RRS feed

  • Question

  • User282693488 posted

    Here is vb code. data table loads OK, listbox does not. data table row has two fields:

    mccode int

    census  decimal (8,0)

    Can anyone please help?

    Dim dt As New DataTable()
            Dim sql = Session("buildquery")
            Dim da As New SqlDataAdapter(sql, ConfigurationManager.ConnectionStrings("BerksConnectionString").ToString())
            Dim RowCount As Integer
            da.Fill(dt)
            Dim dv As DataView = dt.DefaultView
            Int(RowCount = dt.Rows.Count)
            If dt.Rows.Count < 1 Then
                LblError.Text = "no census records were found. Suggest broadening query and retry "
            End If
            ' data table is now loaded with data, works OK
           
           
            ListBox3.DataTextField = "mccode"
            ListBox3.DataValueField = "census"
            ListBox3.DataSource = dt.DefaultView
            ListBox3.DataBind()
           
            'at this point listbox3 should be loasded with data table rows, but it is not

    Sunday, January 5, 2014 8:11 AM

Answers

  • User177399542 posted

    Hi try this :

    ListBox3.DataSource = dt.DefaultView
    ListBox3.DataTextField = "mccode"
    ListBox3.DataValueField = "census"
    ListBox3.DataBind()

    Working Example:

    Protected Sub Page_Load(sender As Object, e As EventArgs)
    	bindListBox()
    End Sub
    
    
    'using System.Data;
    Public Function createDataTable() As DataTable
    	Dim dt As New DataTable()
    	Dim dc As New DataColumn()
    	dc.ColumnName = "Name"
    	dc.DataType = GetType(String)
    	dt.Columns.Add(dc)
    
    	dc = New DataColumn()
    	dc.ColumnName = "Description"
    	dc.DataType = GetType(String)
    	dt.Columns.Add(dc)
    
    	dc = New DataColumn()
    	dc.ColumnName = "Email"
    	dc.DataType = GetType(String)
    	dt.Columns.Add(dc)
    
    	dc = New DataColumn()
    	dc.ColumnName = "Phone"
    	dc.DataType = GetType(String)
    
    	dt.Columns.Add(dc)
    
    	dt.Rows.Add(New Object() {"Max", "Hello my name is Max and I am software engineer.", "Demo@Gmail.com", "My Phone is 123456789"})
    	dt.Rows.Add(New Object() {"Albert", "Hello my name is Albert and I am software engineer.", "Demo@Gmail.com", "My Phone is 123456789"})
    	dt.Rows.Add(New Object() {"Destin", "Hello my name is Destin and I am software engineer.", "Demo@Gmail.com", "My Phone is 123456789"})
    	dt.Rows.Add(New Object() {"Jessie", "Hello my name is Jessie and I am software engineer.", "Demo@Gmail.com", "My Phone is 123456789"})
    	dt.Rows.Add(New Object() {"Joe", "Hello my name is Joe and I am software engineer.", "Demo@Gmail.com", "My Phone is 123456789"})
    	dt.Rows.Add(New Object() {"Dillon", "Hello my name is Dillon and I am software engineer.", "Demo@Gmail.com", "My Phone is 123456789"})
    	dt.Rows.Add(New Object() {"Aldo", "Hello my name is Aldo and I am software engineer.", "Demo@Gmail.com", "My Phone is 123456789"})
    	dt.Rows.Add(New Object() {"Peter", "Hello my name is Peter and I am software engineer.", "Demo@Gmail.com", "My Phone is 123456789"})
    	dt.Rows.Add(New Object() {"Owen", "Hello my name is Owen and I am software engineer.", "Demo@Gmail.com", "My Phone is 123456789"})
    	dt.Rows.Add(New Object() {"Orion", "Hello my name is Orion and I am software engineer.", "Demo@Gmail.com", "My Phone is 123456789"})
    
    	Return dt
    
    End Function
    
    
    Public Sub bindListBox()
    	lstBoxTest.DataSource = createDataTable()
    	lstBoxTest.DataTextField = "Description"
    	lstBoxTest.DataValueField = "Name"
    
    	lstBoxTest.DataBind()
    End Sub
    
    
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 6, 2014 11:42 PM