Display content in Listbox

042355C

Active member
Joined
Nov 30, 2006
Messages
30
Programming Experience
Beginner
Hi to all,

I have a ODBC table call [hflAdvisoryReports]. In it, I wish to fill the listbox with a column name called [RepCode]. Can someone teach me how to do it? Thanks alot in advance.
 
Hi 042355C,

I make a new Windows application project and add a ListBox1 and Button1 controls. The code looks below:

VB.NET:
Imports System.Data.Odbc

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim sql As String = "SELECT RepCode FROM hflAdvisoryReports"
        Dim cn As New OdbcConnection("DSN=MyDataSource")
        Dim cm As New OdbcCommand(sql, cn)
        Dim da As New OdbcDataAdapter(cm)
        Dim ds As New DataSet

        da.Fill(ds, "hflAdvisoryReports")

        ListBox1.DataSource = ds.Tables("hflAdvisoryReports")
        ListBox1.ValueMember = "RepCode"
        ListBox1.DisplayMember = "RepCode"

        cm = Nothing
        da = Nothing
        ds = Nothing
        cn.Close()
    End Sub
End Class

Thank's,
Inhua
 
it did not work. the program failed to run.

Error occured at : da.Fill(ds, "hflAdvisoryReports")

An unhandled exception of type 'System.Data.Odbc.OdbcException' occurred in system.data.dll
Additional information: System error.
 
Back
Top