I would like to get assistance on how to retrieve data from MS SQL database to view it on a listbox.In my db i have two columns.

I would like to get assistance on how to retrieve data from MS SQL database to view it on a listbox.In my db i have two columns.

Hi khuks,

You may use ExecuteReader to retrieve the data from the database..

I would like to get assistance on how to retrieve data from MS SQL database to view it on a listbox.In my db i have two columns.

VB.Net Syntax
Dim mycon As SqlConnection
Dim mycmd As SqlCommand
Dim myquery As String = ""

mycon = New SqlConnection(" . . . ")
myquery = ("SELECT * FROM MYTABLE")
mycon.Open()

mycmd = New SqlCommand(myquery,mycon)

Dim dr as SqlDataReader

mycmd.ExecuteNonQuery
dr = mycmd.ExecuteReader

While dr.Read

Listbox1.items.add(dr.GetString(0)) ' GetString(0) returns data from first column from the result of ur query..
ListBox2.add(dr.GetString(1)) ' Use GetString if item is String, If int then GetInt32 , etc...

End While
mycon.Close()
dr.Close()

I have tried this code and now its just showing a blank listbox

Dim Adp as new SqlDataAdapter("select eno,ename from emp","Data source=......your_connection_string")
Dim Dt as new DataTable

Adp.Fill(Dt)
Listbox1.Datasource=Dt
ListBox1.DisplayMember=Dt.Columns(0).ColumnName '0 for the eno - first column
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.