Question

I have a vb.net program that uses MySQL database. Now I changed the database into Microsoft Access 2010. I changed all necessary settings like connection preference, commands etc.

The issue is: I have got an error around

mydata = mycommand.ExecuteReader

and it generates an error:

Error 1 Value of type 'System.Data.OleDb.OleDbDataReader' cannot be converted to 'MySql.Data.MySqlClient.OleDb.OleDbDataReader'

The entire code is:

 Public Sub showAlarm()
        Try
            Dim conn As New OleDb.OleDbConnection(connection)
            Dim myadapter As New OleDb.OleDbDataAdapter
            conn.Open()
            Dim sqlquery = "select * from alarm"
            Dim mycommand As New OleDb.OleDbCommand
            mycommand.Connection = conn
            mycommand.CommandText = sqlquery
            myadapter.SelectCommand = mycommand
            Dim mydata As MySqlDataReader
            mydata = mycommand.ExecuteReader
            'If mydata.HasRows = 0 Then
            '    MsgBox("Data Not Found")
            '    TextBox1.Clear()
            'Else
            mydata.Read()
            TextBox1.Text = mydata.Item("subhi")
            TextBox2.Text = mydata.Item("zuhur")
            TextBox3.Text = mydata.Item("aser")
            TextBox4.Text = mydata.Item("megrib")
            TextBox5.Text = mydata.Item("isha")
            'End If
            conn.Close()
        Catch ex As Exception
            MessageBox.Show("There is a problem with your connection!")
        End Try

    End Sub

How to solve this problem?

Was it helpful?

Solution

Since mycommand is a OleDbCommand mycommand.ExecuteReader returns a OleDbDataReader but mydata is a MySqlDataReader. So change mydata to be also a OleDbDataReader.

Dim mydata As OleDbDataReader = mycommand.ExecuteReader()

Side-note: you should use the Using-statement to ensure that all unmanaged resources are disposed:

Using conn As New OleDb.OleDbConnection(connection)
    Using mycommand As New OleDb.OleDbCommand("select * from alarm", conn)
        conn.Open()
        Using mydata = mycommand.ExecuteReader()
            If mydata.Read Then
                ' ... '
            End If
        End Using
    End Using
End Using
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top