0

I have this bizarre mistake which I can't find a solution yet. I have a function which I envoke every time when I have to insert data in database. And its actually working because when i check the database entries they are actually there.

However I have this annoying mistake "There is already an open data reader assosiated with connection which must be closed". and I can't find a way to get read of it.

This is the function:

Sub InputDataValues(ByVal StrQwery As String)
    Try
        myconn.Open()
        Dim stquery As String = StrQwery
        Dim smd As MySqlCommand
        smd = New MySqlCommand(stquery, myconn)
        smd.ExecuteReader()

        smd.ExecuteReader().Close()//I added this when I wanted to get rid if the mistake
        myconn.Close()

    Catch ex As Exception
        Dim ErrorMessage As String = "alert('" & ex.Message.ToString() & "');"
        Page.ClientScript.RegisterStartupScript(Me.GetType(), "ErrorAlert", ErrorMessage, True)
        myconn.Close()
    End Try
    myconn.Close()
End Sub

And this is how I call it several times down in the code:

InputDataValues(StrQwery)

If anyone cam give me a hint I will be most grateful.

This is how it look when i changed it to ExecuteNonQuery

Sub InputDataValues(ByVal StrQwery As String)
    Try
        myconn.Open()
        Dim stquery As String = StrQwery
        Dim retValue As Integer
        Dim smd As MySqlCommand
        smd = New MySqlCommand(stquery, myconn)
        retValue = smd.EndExecuteNonQuery()

        myconn.Close()

    Catch ex As Exception
        Dim ErrorMessage As String = "alert('" & ex.Message.ToString() & "');"
        Page.ClientScript.RegisterStartupScript(Me.GetType(), "ErrorAlert", ErrorMessage, True)
        myconn.Close()
    End Try
    myconn.Close()
End Sub
4

1 回答 1

0

您可以在插入数据时用 ExecuteNonQuery 替换 ExecuteReader。在你的 coede 中,我的猜测是每次你调用 ExecuteReader 你都会得到一个新的阅读器,所以你应该在你创建的第一个阅读器上调用 close。

于 2013-04-28T01:16:03.840 回答