I am trying to populate a combobox from a dataset but I am getting the following error: System.NullReferenceException, object reference not set to an instance of an object.
here's my code, can't see where I failed to instantiate.
Dim dad = New OleDbDataAdapter
Dim ds As DataSet
Dim strConn, strSQL As String
strConn = "Data Source=Data3;Integrated Security=SSPI"
strSQL = "Select instNbr from MyTable"
dad = New SqlClient.SqlDataAdapter(strSQL, strConn)
ds = New DataSet
dad.Fill(ds)
cboInstNbr.DataSource = ds.Tables("MyTable")
cboInstNbr.DisplayMember = ds.Tables("MyTable").Columns("instNbr").ToString
the error occurs on the displaymember line of code.
No error message but my combobox is not populated with the field. I am under the assumption that displaymember is the field name your populating the combobox with. So how come it won't populate?
When I added defaultview it gave me the aformentioned error on the datasource line instead this time. I tested the code to see if it would populate a data grid and it had no problem doing that. It just won't work on a combobox. Is there something else that needs to be done for a combobox to get it running?
I'm probably nearly as new as you but I don't think that Pirate's suggestions would deal with the error message you had. It does puzzle me that the error message disappeared at one stage. Without seeing your entire code I have to make a few guesses.
I assume you have imported the OLEDB namespace. If not ,
Dim dad = New OleDbDataAdapter
should be
Dim dad = New Oledb.OleDbDataAdapter
Also, instead of
dad.Fill(ds)
try
dad.Fill(ds, "myTable").
Make sure you have opened your connection
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.