Hi All
i need a bit of help with the code in this function.
basically what the function does is return a datatable which is a result of
the Sql query that is sent to the function
Although it works, I dont like the 'If Not Reader.IsClosed Then Reader.Close()
the only reason that it is there is that if i take it out then the function executes
fine the first time but thereafter i get the error:
'There is already an open datareader associated with this connection'
I am sure there must be a better way of writing this code.
Any help would be greatly appreciated.
Public Function GetResultSet(ByVal SqlQuery As String) As DataTable
'This Function Returns as table of data based
'on the sql query passed into the function
GetResultSet = Nothing
Dim Reader As MySqlDataReader
Try
Dim Table As New DataTable
Dim cmd As New MySqlCommand(SqlQuery, Datacon)
Reader = cmd.ExecuteReader
If Reader.HasRows Then
Table.Load(Reader)
Return Table
End If
Catch DBError As Exception
Msgbox("Error Retrieving Data")
Finally
If Not Reader.IsClosed Then Reader.Close()
End Try
i need a bit of help with the code in this function.
basically what the function does is return a datatable which is a result of
the Sql query that is sent to the function
Although it works, I dont like the 'If Not Reader.IsClosed Then Reader.Close()
the only reason that it is there is that if i take it out then the function executes
fine the first time but thereafter i get the error:
'There is already an open datareader associated with this connection'
I am sure there must be a better way of writing this code.
Any help would be greatly appreciated.
Public Function GetResultSet(ByVal SqlQuery As String) As DataTable
'This Function Returns as table of data based
'on the sql query passed into the function
GetResultSet = Nothing
Dim Reader As MySqlDataReader
Try
Dim Table As New DataTable
Dim cmd As New MySqlCommand(SqlQuery, Datacon)
Reader = cmd.ExecuteReader
If Reader.HasRows Then
Table.Load(Reader)
Return Table
End If
Catch DBError As Exception
Msgbox("Error Retrieving Data")
Finally
If Not Reader.IsClosed Then Reader.Close()
End Try