I am a beginner trying to make a networked game called Guess Who.
I have 2 combo boxes
I am trying to read a access database. Whatever is selected in the 1st combo box will be the column to 'lookup' the values in the database. These values should go into the 2nd combo box. I keep getting this error:"Conversion from type 'DBNull' to type 'String' is not valid" on line 21 when I select "Eye Color" in the 1st combo box. I'm guessing its because its reading the null value after the reading "Brown".
How do you stop this from happening?
My database looks like the following:
The column names are in between the [ ] sorry don't know how to format this..
[Eye Color] [Hair Color] Blue Black Brown Blonde Brown Red
This is the code I have to read the database:
Public Sub Question_Update() Dim Cmd As OleDb.OleDbCommand Dim Con As OleDb.OleDbConnection Dim Sql As String = Nothing Dim Reader As OleDb.OleDbDataReader Dim ComboRow As Integer = -1 Dim Columns As Integer = 0 Dim Category As String = Nothing Dim based As String = cmbFeatures.Text cmbQuestion.Items.Clear() Con = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Questions.mdb") Sql = "SELECT" & " [" & based & "] " & "FROM [Questions]" Cmd = New OleDb.OleDbCommand(Sql, Con) Con.Open() Reader = Cmd.ExecuteReader() While Reader.Read() For Columns = 0 To Reader.FieldCount - 1 MsgBox(Reader.Item(Columns).ToString) Category = Reader.Item(Columns) 'READ COLUMN FROM DATABASE Next cmbQuestion.Items.Add(Category) ComboRow += 1 End While Con.Close() End Sub