help guys. im having this error.. i don't know hat to do.. please help..thankss

#Region "FillListView"

Sub FillListview()
lvList.Items.Clear()
sqlQry = "SELECT * from book ORDER BY book_id ASC"
Dim mycmd As New MySqlCommand(sqlQry, myConn)
Dim dReader As MySqlDataReader = mycmd.ExecuteReader() <----
this is the error im getting it says invalid operation exception was unhandled and must be open and valid --->

While dReader.Read()
With lvList
.Items.Add(dReader("book_id"))
With .Items(.Items.Count - 1).SubItems
.Add(dReader("book_title"))
.Add(dReader("book_author"))
.Add(dReader("book_publisher"))
.Add(dReader("isbn"))
.Add(dReader("call_no"))
End With
End With
End While
dReader.Close()
End Sub
#End Region

Dim dReader As MySqlDataReader = mycmd.ExecuteReader()

It seems that your reader is from a class module. It is either not open in the context you have shown, so it can not execute any operations. Post us the code under the MySqlDataReader. This might show us why there is no connection.

this is the whole code of the bookform:
*****************************************************************************

Imports MySql.Data.MySqlClient

Public Class bookForm

    Dim myConn As New MySqlConnection
    Dim sqlQry As String = Nothing
    Dim mycmd As New MySqlCommand
    Dim dReader As MySqlDataReader
    'Dim dAdap As New MySqlDataAdapter
    'Dim dSet As New DataSet

    Dim connString As String
    Dim str As String = Nothing

    Private Sub bookForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Call FillListview()
        Call LVsettings()
        Call ConnToDB()
        Call FillListview()
        Call Set1()

    End Sub
#Region "FillListView"

    Sub FillListview()
        lvList.Items.Clear()
        sqlQry = "SELECT * from book ORDER BY book_id ASC"
        mycmd = New MySqlCommand(sqlQry, myConn)
        myConn.Open()
        dReader = mycmd.ExecuteReader

        While dReader.Read
            With lvList
                .Items.Add(dReader("book_id"))
                With .Items(.Items.Count - 1).SubItems
                    .Add(dReader("book_title"))
                    .Add(dReader("book_author"))
                    .Add(dReader("book_publisher"))
                    .Add(dReader("isbn"))
                    .Add(dReader("call_no"))
                End With
            End With
        End While
        dReader.Close()
        myConn.Close()
    End Sub
#End Region

#Region "Connection"
    Sub ConnToDB()
        Try
            With myConn
                If .State = ConnectionState.Open Then .Close()
                .ConnectionString = "datasource=localhost; username=root; password=lani; database=admin"
                .Open()
            End With
        Catch ex As Exception
            MessageBox.Show("Unable to connect", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Application.Exit()
        End Try
    End Sub
#End Region


#Region "LVSettings"
    Sub LVsettings()
        With lvList.Columns
            .Add("book_id", 50, HorizontalAlignment.Left)
            .Add("book_title", 200, HorizontalAlignment.Left)
            .Add("book_author", 200, HorizontalAlignment.Left)
            .Add("isbn", 100, HorizontalAlignment.Left)
            .Add("call_no", 100, HorizontalAlignment.Left)
        End With
    End Sub
#End Region

#Region "object Settings "

    Sub Set1()
        cmdAdd.Enabled = True
        cmdEdit.Enabled = True
        cmddelete.Enabled = True
        cmdExit.Enabled = True

        cmdsave.Enabled = False
        cmdCancel.Enabled = False
        Dim txt As Control
        For Each txt In Me.Controls
            If TypeOf txt Is TextBox Then
                txt.Enabled = False
            End If
        Next

        lvList.Enabled = True
    End Sub

    Sub Set2()
        cmdAdd.Enabled = False
        cmdEdit.Enabled = False
        cmddelete.Enabled = False
        cmdExit.Enabled = False

        cmdsave.Enabled = True
        cmdCancel.Enabled = True

        Dim txt As Control
        For Each txt In Me.Controls
            If TypeOf txt Is TextBox Then
                txt.Enabled = True
            End If
        Next
        lvList.Enabled = False
    End Sub
#End Region

#Region "CLEAR TEXTBOX"
    Sub ClearAlltextBox()
        Dim a As Control
        For Each a In Me.Controls
            If TypeOf a Is TextBox Then
                a.Text = Nothing
            End If
        Next
    End Sub
#End Region



    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
        str = "add"
        Call Set2()
        Call ClearAlltextBox()
        idtxt.Text = "Computer Generated"
        idtxt.Enabled = False
    End Sub

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        myConn.Close()
        Me.Close()
        Call Set1()
        Call ClearAlltextBox()

    End Sub

    Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        If idtxt.Text = "" Then
            MessageBox.Show("Please Select Record to Update", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            str = "edit"
            Call Set2()
            idtxt.Enabled = False
        End If


    End Sub

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Application.Exit()
    End Sub

    Private Sub cmdsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsave.Click
        If str = "add" Then
            ''''''ADD NEW RECORD'''''''
            If idtxt.Text = "" Or titletxt.Text = "" Or authortxt.Text = "" Or Pubtxt.Text = "" Or isbntxt.Text = "" Or calltxt.Text Then
                MessageBox.Show("All fields Are Required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                sqlQry = "INSERT INTO book(book_id,book_title,book_author,book_publisher,isbn,call_no) "
                sqlQry = sqlQry + "VALUES('" & idtxt.Text & "','" & titletxt.Text & "','" & authortxt.Text & "','" & Pubtxt.Text & "','" & isbntxt.Text & "','" & calltxt.Text & "')"
                mycmd = New MySqlCommand
                With mycmd
                    .CommandText = sqlQry
                    .Connection = myConn
                    .ExecuteNonQuery()
                End With
                Call Set1()
            End If

        Else
            ''''''''''UPDATE RECORD'''''''
            If idtxt.Text = "" Or titletxt.Text = "" Or authortxt.Text = "" Or Pubtxt.Text = "" Or isbntxt.Text = "" Or calltxt.Text Then
                MessageBox.Show("All fields Are Required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                sqlQry = "UPDATE book SET "
                sqlQry = sqlQry + " book_title = '" & titletxt.Text & "',"
                sqlQry = sqlQry + " book_author = '" & authortxt.Text & "',"
                sqlQry = sqlQry + " book_publisher = '" & Pubtxt.Text & "',"
                sqlQry = sqlQry + " isbn = '" & isbntxt.Text & "',"
                sqlQry = sqlQry + " call_no = '" & authortxt.Text & "'"
                sqlQry = sqlQry + " WHERE "
                sqlQry = sqlQry + " book_id = " & idtxt.Text

                mycmd = New MySqlCommand(sqlQry, myConn)
                mycmd.ExecuteNonQuery()
                Call Set1()
            End If
        End If

        Call FillListview()
        Call ClearAlltextBox()

    End Sub

    Private Sub cmddelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmddelete.Click
        If MsgBox("Are you sure you want to delete this record?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            sqlQry = "DELETE FROM book WHERE book_id = " & idtxt.Text
            mycmd = New MySqlCommand(sqlQry, myConn)
            mycmd.ExecuteNonQuery()
        End If
        Call ClearAlltextBox()
        Call FillListview()
    End Sub

    Private Sub lvList_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvList.SelectedIndexChanged
        If lvList.SelectedItems.Count > 0 Then
            With lvList.SelectedItems(0)
                idtxt.Text = .Text
                titletxt.Text = .SubItems(1).Text
                authortxt.Text = .SubItems(2).Text
                Pubtxt.Text = .SubItems(3).Text
                isbntxt.Text = .SubItems(4).Text
                calltxt.Text = .SubItems(5).Text
            End With
        End If
    End Sub
End Class

i put myconn.open as shown at line33 but an error occured and says: argument null exception was unhandled. key cannot be null


help i dont know what to do.. thanksss

a Quick glance at your code seems fine. It is with your reader.

Dim dReader As MySqlDataReader = mycmd.ExecuteReader() <----
this is the error im getting it says invalid operation exception was unhandled and must be open and valid --->

Try and change MySqlDataReader line to the following -

Dim dReader As New MySqlDataReader
dReader = mycmd.ExecuteReader

, and see if it helps. I am assuming that MySqlDataReader is a foreign control which was referenced through references?

#Region "FillListView"

    Sub FillListview()
        lvList.Items.Clear()
        sqlQry = "SELECT * from book ORDER BY book_id ASC"
        Dim mycmd As New MySqlCommand(sqlQry, myConn)
        Try
            myConn.Open()
            Dim dReader As MySqlDataReader
            dReader = mycmd.ExecuteReader
            While dReader.Read()
                With lvList
                    .Items.Add(dReader("book_id"))
                    With .Items(.Items.Count - 1).SubItems
                        .Add(dReader("book_title"))
                        .Add(dReader("book_author"))
                        .Add(dReader("book_publisher"))
                        .Add(dReader("isbn"))
                        .Add(dReader("call_no"))
                    End With
                End With
            End While
            dReader.Close()
            myConn.Close()
        Catch ex As MySqlException
        End Try
    End Sub
#End Region

yes i tried what you said as shown above. but when i put the myconn.open its saying the 'null reference exception was unhandled' object reference not set to an instance of an object

im researching and debugging this error for a while now.. just cant figure it out yet.. can you please help me again :D thanksss much

Edited 6 Years Ago by lanitooot: n/a

That is it seems because you are trying to open 'myConn' without setting a reference to it -

Dim mycmd As New MySqlCommand(sqlQry, myConn)
Try
myConn.Open()

Set a reference to myConn ABOVE this code as you did with sqlQry and see if that works.

whew!.. it worked..i hav tried try and catch method maybe i dont know wer to put it thenn.. now it worked..thanks much andreret

Only a pleasure. If you have any more problems, please post it under .Net for us, seeing that you are using .Net. Thanks man, and happy coding.

This question has already been answered. Start a new discussion instead.