Question

I know there are a lot of threads on here regarding my error but I can't find an answer that helps me directly.

I have created an SQL class so I can pass my queries and receive data back rather than calling a separate routine everytime. The code for my class looks like so:

Imports MySql.Data.MySqlClient

Public Class SQLHandler

    Public Function ReturnData(ByVal strSql As String) As DataTable
        'method receives an sql query string and returns a dataTable
        Try
            Using ExQry As New MySqlCommand(strSql, MySQLConn)
                Using da As New MySqlDataAdapter(ExQry)
                    Dim tempDT As New DataTable

                    da.Fill(tempDT)
                    da.Dispose()
                    ExQry.Dispose()

                    Return tempDT
                End Using
            End Using

        Catch ex As Exception
            CreateLog("Module: ReturnData()" & vbNewLine & "Exception Error: " & ex.Message)
            MsgBox("Exception Error: " & ex.Message, MsgBoxStyle.Critical, "Module: ReturnData()")
            Return Nothing
        End Try
    End Function

    Public Function ReturnIntValue(ByVal strSql As String) As Integer
        'method receives an sql query string and returns a integer
        Try
            Using ExQry As New MySqlCommand(strSql, MySQLConn)
                Dim result As Integer = Convert.ToInt32(ExQry.ExecuteScalar())

                ExQry.Dispose()

                Return result
            End Using

        Catch ex As Exception
            CreateLog("Module: ReturnIntValue()" & vbNewLine & "Exception Error: " & ex.Message)
            MsgBox("Exception Error: " & ex.Message, MsgBoxStyle.Critical, "Module: ReturnIntValue()")
            Return Nothing
        End Try
    End Function
End Class

One function is to return a datatable and the second function returns me a single value. The problem I have is that my code will make various calls to this class (one after the other), sometimes returning a datatable and sometimes returning a value. However, I am finding that if I request a query straight after another query, I am receiving an error about already having an open datareader.

I don't know how to prevent my code from waiting until the query has finished and closed before running another one.

I keep getting the error in the ReturnIntValue function, so I'm assuming it is calling this routine before a datatable has been returned.

Any help would be appreciated. Obviously I am doing something incorrect. Thanks

Was it helpful?

Solution 2

Found a great article here [http://www.developerfusion.com/code/5445/sql-data-provider-vbnet-class/] which provided details on setting up a proper SQL Class to handle multiple connections throughout my application.

OTHER TIPS

Try This getting Single Data... Without using Reader (Basically it is safe not to use Reader, because it may cause the DB to unstable when more computer are connected to it (Avoid DB.Close() )

Dim SerialMac As New SqlConnection(CommandString)
Dim CMD As New SqlCommand
Dim sqlAdapter As SqlDataAdapter


  Public Function getDataInDB(ByVal sQuery As String) As String
    getDataInDB = Nothing
    Try
        CMD = New SqlCommand(sQuery, TestResult)
        Dim sqlAdapter As SqlDataAdapter
        Dim dataS As DataSet
        sqlAdapter = New SqlDataAdapter(CMD)
        dataS = New DataSet
        sqlAdapter.Fill(dataS, "getRecord")
        getDataInDB = dataS.Tables("getRecord").Rows(0).ItemArray(0).ToString()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Function

Plus Display data on DataGridview

Public Sub displayTestResults(ByRef mDataGridView As DataGridView, ByVal sQuery As String)
    Try
        connectToDB()
        CMD = New SqlCommand(sQuery, TestResult)
        Dim sqlAdapter As SqlDataAdapter
        Dim dataS As DataSet
        sqlAdapter = New SqlDataAdapter(CMD)
        dataS = New DataSet
        sqlAdapter.Fill(dataS, "Records")
        mDataGridView.DataSource = dataS.Tables("Records")
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top