cmd = New SqlCommand("SELECT UserName FROM ............... sql go here ....", con)
cmd.Parameters.AddWithValue("@IDAutoNumber", AutoNumber)
con.Open()
reader = cmd.ExecuteReader()
cmd.Parameters.Clear()
If reader.HasRows Then
While reader.Read()
'Here we get UserName from each record read
NewReadUserName = CType(reader("UserName"), Object).ToString
'Here we update table for particular UserName
cmd = New SqlCommand("UPDATE RegistrationList .............sql go here .........", con)
cmd.Parameters.AddWithValue("@AutoNumber", AutoNumber)
cmd.Parameters.AddWithValue("@NewReadUserName", NewReadUserName)
If NumberOfRecordsFound = 1 Then
cmd.Parameters.AddWithValue("@WordToEnter", "Found one")
ElseIf NumberOfRecordsFound > 1 Then
cmd.Parameters.AddWithValue("@WordToEnter", "Found more than one")
End If
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
End While
End If
reader.Close()
con.Close()
I'm receiving this message when I try to access the page:
Server Error in '/' Application.
There is already an open DataReader associated with this Command which must be closed first.
Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details:System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.
Source Error:
Line 164: cmd.Parameters.AddWithValue("@WordToEnterForFirstPlace", "TieFirstPlaceWinner")
Line 165: End If
Line 166: cmd.ExecuteNonQuery()
Line 167: cmd.Parameters.Clear()
Line 168:
Since cmd is already used by SQLDataReader, hence you cannot use it without closing DataReader.
Hence solution is to create a different object cmd2
cmd = New SqlCommand("SELECT UserName FROM ............... sql go here ....", con)
cmd.Parameters.AddWithValue("@IDAutoNumber", AutoNumber)
con.Open()
reader = cmd.ExecuteReader()
cmd.Parameters.Clear()
If reader.HasRows Then
While reader.Read()
'Here we get UserName from each record read
NewReadUserName = CType(reader("UserName"), Object).ToString
'Here we update table for particular UserName
Using cmd2 As New SqlCommand("UPDATE RegistrationList .............sql go here .........", con)
cmd2.Parameters.AddWithValue("@AutoNumber", AutoNumber)
cmd2.Parameters.AddWithValue("@NewReadUserName", NewReadUserName)
If NumberOfRecordsFound = 1 Then
cmd2.Parameters.AddWithValue("@WordToEnter", "Found one")
ElseIf NumberOfRecordsFound > 1 Then
cmd2.Parameters.AddWithValue("@WordToEnter", "Found more than one")
End If
cmd2.ExecuteNonQuery()
cmd2.Parameters.Clear()
End Using
End While
End If
reader.Close()
con.Close()
As the error indicates you cannot invoke a query on the same connection the reader is using to fetch records. Open a new connection to handle the second query or let the reader finish then iterate over the result.
Member
58 Points
376 Posts
Error with DataReader associated with Command
Jul 26, 2018 01:42 PM|vstorpedo|LINK
I do have the following in code behind:
I'm receiving this message when I try to access the page:
Server Error in '/' Application.
There is already an open DataReader associated with this Command which must be closed first.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.
Source Error:
Line 164: cmd.Parameters.AddWithValue("@WordToEnterForFirstPlace", "TieFirstPlaceWinner") Line 165: End If Line 166: cmd.ExecuteNonQuery() Line 167: cmd.Parameters.Clear() Line 168:
Source File: C:\bGlocal\TalentShow\AdminShowsVotingResultPage.aspx Line: 166
Stack Trace:
I can not close the reader before it start reading. What should I do here?
Thanks to all.
All-Star
54508 Points
14111 Posts
Re: Error with DataReader associated with Command
Jul 26, 2018 02:01 PM|mudassarkhan|LINK
Since cmd is already used by SQLDataReader, hence you cannot use it without closing DataReader.
Hence solution is to create a different object cmd2
Blog: ASPSnippets | Forum: ASPForums | Company: Excelasoft
All-Star
53121 Points
23672 Posts
Re: Error with DataReader associated with Command
Jul 26, 2018 02:02 PM|mgebhard|LINK
As the error indicates you cannot invoke a query on the same connection the reader is using to fetch records. Open a new connection to handle the second query or let the reader finish then iterate over the result.
Member
58 Points
376 Posts
Re: Error with DataReader associated with Command
Jul 26, 2018 02:20 PM|vstorpedo|LINK
Sorry mudassarkhanbut I tried that before and didn't work. The right answer is the following: "As the error indicates you cannot invoke a query on the same connection the reader is using to fetch records. Open a new connection to handle the second query or let the reader finish then iterate over the result."
Thanks for your help, I really appreciated. Thanks again
All-Star
54508 Points
14111 Posts
Re: Error with DataReader associated with Command
Jul 26, 2018 04:26 PM|mudassarkhan|LINK
Yes. I did not open a new Connection. That is a mistake. NP
Blog: ASPSnippets | Forum: ASPForums | Company: Excelasoft