I have a problem with a combobox and no page on the Internet has been able to help me so far. So I hope that one of you can.

I have a form (with information about clients) with textboxes binded to a dataview. One of the columns in the dataview contains a number that tells me what kind of client it is. I don't my users want that my users have to fill in a number in a textbox, but I want them to choose a item from a combobox. De selected value should be the value that is actually saved in the database.

This is my code:

Dim cmd As New SqlCommand
Dim dr As SqlDataReader


cmd.Connection = cnnVraagbaak
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "relatie.proc_OpvragenRelatiesoort"
daRelatie.SelectCommand = cmd
daRelatie.Fill(dsRelatie, "Relatiesoort")

cbo.DataSource = dsRelatie.Tables("Relatiesoort")
cbo.ValueMember = dsRelatie.Tables("Relatiesoort").Columns(0).ToStri ng
cbo.DisplayMember = dsRelatie.Tables("Relatiesoort").Columns(1).ToStri ng
cbo.DataBindings.Add("SelectedValue", dsRelatie.Tables("Relaties"), "Relatiesoort")

De Datasource is the information in my combobox. With Databindings I'm trying to bind it to my dataset (or view, that doesn't work either) with clients. Every time I get a NullReferenceExeption. The moment I remove the line cbo.DataSource = dsRelatie.Tables("Relatiesoort"), I don't get an error, but it doesn't work either.

That dataset is filled (I've checked) and also the derived view. The column Relatiesoort exists.

What do I have to do to make it possible for my users to choose an item from a combobox and save the selected value to the underlying dataset?

Try this:

dim dtTable as Datatable
dim drRow as DataRow
dim intRecord as integer
'assume you populated the table etc

for intRecord = 0 to dtTable.Rows.count -1
drRow = dtTable.Rows(intRecord)
myCombox.Items.Add(New DataItem(drRow(0).Tostring, drRow(1).ToString)

You say the 'column' relatiesoort exists, but what is the name/index of the table in the dataset? When you use a dataset for the datasrouce you specify the table to use eg ComboBox1.DataSource = ds.Tables("tablename") or ComboBox1.DataSource = ds.Tables(0) . Once you have assigned the table you then set the Comboboxes Display and Value members:

ComboBox1.DisplayMember = "relatiesoort"
        ComboBox1.ValueMember = "nameOfColumn"

The display member is the value shown in combobox and the valuemember is returned by ComboBox1.SelectedValue

daRelatie.Fill(dsRelatie, "Relatiesoort")

cbo.DataSource = dsRelatie.Tables("Relatiesoort")
cbo.DisplayMember ="columnnsme"
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.