G
Guest
Hi,
I keep getting the following error message:
InvalidOperationException: There is already an open DataReader associated
with this Connection which must be closed first.
I have read up about it and realise that you can only have one DataReader
open on a Connection at a time, but don't know how to rewrite my code to work
properly.
Currently I have the following:
Protected Shared myDA As SqlDataAdapter
Protected Shared ReadOnly Property DataAdapter() As SqlDataAdapter
Get
If myDA Is Nothing Then
myDA = New SqlDataAdapter("SELECT * FROM Course",
CData.Connection)
Dim cb As New SqlCommandBuilder(myDA)
myDA.UpdateCommand = cb.GetUpdateCommand()
myDA.InsertCommand = cb.GetInsertCommand()
myDA.DeleteCommand = cb.GetDeleteCommand()
CData.Connection.Close()
End If
Return myDA
End Get
End Property
Public Shared Function GetCourse(ByVal CourseID As Integer) As CCourse
Dim selectText As String = DataAdapter.SelectCommand.CommandText
DataAdapter.SelectCommand.CommandText = "SELECT * FROM Course WHERE
CourseID =" & CourseID
Dim dt As New DataTable
DataAdapter.Fill(dt)
DataAdapter.SelectCommand.CommandText = selectText
If dt.Rows.Count = 0 Then
Return Nothing
Else
Return New CCourse(dt.Rows(0))
End If
End Function
When I try to access the GetCourse Function from two different computers at
the same time it gives the error as mentioned above.
Can anyone help me to solve this problem... i.e. am I writing my code
completely wrong.
Any help would be much appreciated.
Thanks in advance.
Grant.
I keep getting the following error message:
InvalidOperationException: There is already an open DataReader associated
with this Connection which must be closed first.
I have read up about it and realise that you can only have one DataReader
open on a Connection at a time, but don't know how to rewrite my code to work
properly.
Currently I have the following:
Protected Shared myDA As SqlDataAdapter
Protected Shared ReadOnly Property DataAdapter() As SqlDataAdapter
Get
If myDA Is Nothing Then
myDA = New SqlDataAdapter("SELECT * FROM Course",
CData.Connection)
Dim cb As New SqlCommandBuilder(myDA)
myDA.UpdateCommand = cb.GetUpdateCommand()
myDA.InsertCommand = cb.GetInsertCommand()
myDA.DeleteCommand = cb.GetDeleteCommand()
CData.Connection.Close()
End If
Return myDA
End Get
End Property
Public Shared Function GetCourse(ByVal CourseID As Integer) As CCourse
Dim selectText As String = DataAdapter.SelectCommand.CommandText
DataAdapter.SelectCommand.CommandText = "SELECT * FROM Course WHERE
CourseID =" & CourseID
Dim dt As New DataTable
DataAdapter.Fill(dt)
DataAdapter.SelectCommand.CommandText = selectText
If dt.Rows.Count = 0 Then
Return Nothing
Else
Return New CCourse(dt.Rows(0))
End If
End Function
When I try to access the GetCourse Function from two different computers at
the same time it gives the error as mentioned above.
Can anyone help me to solve this problem... i.e. am I writing my code
completely wrong.
Any help would be much appreciated.
Thanks in advance.
Grant.