 |
| ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 3rd, 2011, 04:59 AM
|
|
Registered User
|
|
Join Date: Jun 2011
Posts: 7
Thanks: 1
Thanked 1 Time in 1 Post
|
|
Error: Invalid attempt to call Read when reader is closed
Hi,
I have this sub which performs updates in successive function call. The reader object is closed only when the operation has been completed (after the while loop has ended), yet I get this error. Any suggestions?
Code:
sub reset_slno()
dim objCmd2 as new OleDBCommand()
dim objReader2 as OleDBDataReader
objCmd2.Connection = Conn
objCmd2.CommandText = "SELECT * FROM tbl_civil_works"
try
objCmd2.Connection.Open()
objReader2 = objCmd2.ExecuteReader
catch ex as exception
lblMessage.Text = ex.Message
end try
dim strSQL3 as String
dim record_counter as Integer = 1
while objReader2.Read()
strSQL3 = "UPDATE tbl_civil_works SET codeno = " & record_counter
ExecuteStatement(strSQL3)
record_counter = record_counter + 1
end while
objReader2.Close()
objCmd2.Connection.Close()
end sub
Error details:
Quote:
Source Error:
Line 227: dim record_counter as Integer = 1
Line 228:
Line 229: while objReader2.Read()
Line 230: dim strSQL3 as String = "UPDATE tbl_civil_works SET codeno = " & record_counter
Line 231: ExecuteStatement(strSQL3)
Source File: i:\inetpub\wwwroot\ecsite\civilworks1.aspx Line: 229
|
|
|
The Following User Says Thank You to Vayne For This Useful Post:
|
|
|

June 3rd, 2011, 05:43 AM
|
|
Friend of Wrox
|
|
Join Date: May 2004
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
|
|
Try this:
Code:
If objReader2.HasRows Then
Do While objReader2.Read()
strSQL3 = "UPDATE tbl_civil_works SET codeno = " & record_counter
ExecuteStatement(strSQL3)
record_counter = record_counter + 1
Loop
Else
Console.WriteLine("No rows found.")
End If
__________________
Om Prakash Pant
Click the "Thanks" button if this post helped you.
|
|

June 3rd, 2011, 05:55 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
And what's the error you're getting?
Imar
|
|

June 3rd, 2011, 07:43 AM
|
|
Registered User
|
|
Join Date: Jun 2011
Posts: 7
Thanks: 1
Thanked 1 Time in 1 Post
|
|
Quote:
Originally Posted by om_prakash
Try this:
Code:
If objReader2.HasRows Then
Do While objReader2.Read()
strSQL3 = "UPDATE tbl_civil_works SET codeno = " & record_counter
ExecuteStatement(strSQL3)
record_counter = record_counter + 1
Loop
Else
Console.WriteLine("No rows found.")
End If
|
Still the same error message.
|
|

June 3rd, 2011, 07:44 AM
|
|
Registered User
|
|
Join Date: Jun 2011
Posts: 7
Thanks: 1
Thanked 1 Time in 1 Post
|
|
Quote:
Originally Posted by Imar
And what's the error you're getting?
Imar
|
Error: Invalid attempt to call Read when reader is closed
Its in the thread title. :)
|
|

June 3rd, 2011, 07:48 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Is ExecuteStatement using the same connection? if so, this is not support out of the box. By default, a SqlDataReader requires its own, dedicated open connection to the database. So, when ExecuteStatement excutes, it messes up the connection used by the SqlDatareader.
Two solutions:
1. use a dedicated Connection objct for the SqlDataReader
2. Enable MARS (Multiple Active Result Sets) so multiple readers / data access code can exist on the same connection. Google has examples of enabling MARS.
Cheers,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

June 3rd, 2011, 08:22 AM
|
|
Registered User
|
|
Join Date: Jun 2011
Posts: 7
Thanks: 1
Thanked 1 Time in 1 Post
|
|
Quote:
Originally Posted by Imar
Is ExecuteStatement using the same connection? if so, this is not support out of the box. By default, a SqlDataReader requires its own, dedicated open connection to the database. So, when ExecuteStatement excutes, it messes up the connection used by the SqlDatareader.
Two solutions:
1. use a dedicated Connection objct for the SqlDataReader
2. Enable MARS (Multiple Active Result Sets) so multiple readers / data access code can exist on the same connection. Google has examples of enabling MARS.
Cheers,
Imar
|
Thanks for the reply Imar!
The ExecuteStatement function is using a different connection and is only meant to execute nonquery. I used to it to insert data and also update data (however update is not working and for which very reason I am using this function).
And right now I am using Access database.
Last edited by Vayne; June 3rd, 2011 at 08:35 AM..
|
|

June 3rd, 2011, 08:48 AM
|
|
Registered User
|
|
Join Date: Jun 2011
Posts: 7
Thanks: 1
Thanked 1 Time in 1 Post
|
|
This is the ExecuteStatement function:
Code:
function ExecuteStatement(strSQL as String)
dim objCmd as new OleDBCommand(strSQL, Conn)
Response.Write(strSQL)
try
objCmd.Connection.Open()
objCmd.ExecuteNonQuery
catch ex as exception
lblMessage.Text &= ex.Message
end try
objCmd.Connection.Close()
end function
|
|

June 3rd, 2011, 09:27 AM
|
|
Registered User
|
|
Join Date: Jun 2011
Posts: 7
Thanks: 1
Thanked 1 Time in 1 Post
|
|
Woops! My bad, I am actually using a single connection for both, it was global. I have rectified it now. Thanks!
|
|

June 3rd, 2011, 09:32 AM
|
|
Registered User
|
|
Join Date: Jun 2011
Posts: 7
Thanks: 1
Thanked 1 Time in 1 Post
|
|
*I am just not being insightful*
Last edited by Vayne; June 3rd, 2011 at 11:19 AM..
Reason: Invalid point to make
|
|
 |