It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Visual Basic Forum

Reading an Access Database with VB.net

Moderator: dafunkymunky

Reading an Access Database with VB.net

Postby shuey79 on Sat Oct 20, 2007 6:12 am

Hello,
I am a beginner trying to make a networked game called Guess Who.
I have 2 combo boxes
I am trying to read an 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 "Category = Reader.Item(Columns)" when I select "Eye Color" in the 1st combo box. I'm guessing its because its reading the null value after 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..
Code: Select all

      [Eye Color]     [Hair Color]
         Blue            Black
         Brown           Blonde
                         Brown
                         Red


This is the code I have to read the database:
Code: Select all
      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
      Category = Reader.Item(Columns) 'READ COLUMN FROM DATABASE
      Next
      cmbQuestion.Items.Add(Category)
      ComboRow += 1
      End While
      Con.Close()
        End Sub
shuey79
 
Posts: 0
Joined: Sat Oct 20, 2007 5:29 am

Who is online

Users browsing this forum: No registered users and 4 guests