Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ADO.NET fill listboxes / comboxes

Status
Not open for further replies.

Crookshanks

Technical User
May 18, 2004
296
NL
Hello there!

Small question. I've got a form with a listbox. I do not have object relating to the database on the form. I would like to create the object during runtime.

What is the easiest / best way to populate a listbox / combobox using a SQL Query. I've read it has to be a Ilist, but I am not sure how to start.

Could someone supply some example code for an access database (oledbcommands). Thanks in advance.

Kinds regards,
 
As a simple example (you will have to change the client from a SQLClient to an OleDb), how about:
Code:
        Dim MyDataReader As System.Data.SQLClient.SqlDataReader
        Dim MyCommand As System.Data.SQLClient.SqlCommand
        Dim MyConnection As System.Data.SQLClient.SqlConnection

        MyConnection = New System.Data.SQLClient.SqlConnection("server=127.0.0.1; initial catalog=Northwind;uid=USERNAME;pwd=PASSWORD")
        MyConnection.Open()
        MyCommand = New System.Data.SQLClient.SqlCommand
        MyCommand.Connection = MyConnection
        MyCommand.CommandText = "SELECT ProductName FROM products"
        MyDataReader = MyCommand.ExecuteReader()

        While MyDataReader.Read
            ListBox1.Items.Add(MyDataReader.Item("ProductName"))
        End While

        MyDataReader = Nothing
        MyCommand = Nothing
        MyConnection.Close()
        MyConnection = Nothing


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Public conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\QTrace.mdb;Persist Security Info=False;"
Public oledbcon As New OleDbConnection(conn)

Records tonen :

oledbcon.Open()
Dim adapterRelaties As New OleDbDataAdapter("Select * from Relaties", oledbcon)
adapterRelaties.Fill(datasetRelaties)
If datasetRelaties.Tables(0).Rows.Count = 0 Then
MessageBox.Show("Geen records gevonden!")
Else
DataGridView1.DataSource = datasetRelaties.Tables(0)
ComboBox1.DataSource = datasetRelaties.Tables(0)
ComboBox1.DisplayMember = "Naam"
ListBox1.DataSource = datasetRelaties.Tables(0)
ListBox1.DisplayMember = "Naam"
TextBox1.DataBindings.Add("Text", datasetRelaties.Tables(0), "Naam")
End If
oledbcon.Close()


Eric De Decker
Visit The Belgium "Visual Basic Group" at
 
Thanks to you both for your reply. With this info I should be able to achieve my goal. Regards.
 
I've been trying to implement the first example code in my testprogram and the follow line gives the following error:

MyCommand.CommandText = "SELECT Id, Club FROM Table1"

An unhandled exception of type 'System.NullReferenceException' occurred in ADOgerommel.exe
Additional information: Object reference not set to an instance of an object.

What does it mean and what can I do about it. I checked the Table1 and it seems to be in order.
 
That means that the MyCommand object is not instantiated. Are you sure it is defined as New or set = to New or to an existing object?

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
You are absoluterly right. I splitted the declaration and the initialisation of the instance in a wrong way. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top