Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 3rd, 2011, 04:59 AM
Registered User
 
Join Date: Jun 2011
Posts: 7
Thanks: 1
Thanked 1 Time in 1 Post
Default 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
Reply With Quote
The Following User Says Thank You to Vayne For This Useful Post:
  #2 (permalink)  
Old June 3rd, 2011, 05:43 AM
Friend of Wrox
Points: 2,473, Level: 20
Points: 2,473, Level: 20 Points: 2,473, Level: 20 Points: 2,473, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2004
Location: India
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

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.
Reply With Quote
  #3 (permalink)  
Old June 3rd, 2011, 05:55 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

And what's the error you're getting?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #4 (permalink)  
Old June 3rd, 2011, 07:43 AM
Registered User
 
Join Date: Jun 2011
Posts: 7
Thanks: 1
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by om_prakash View Post
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.
Reply With Quote
  #5 (permalink)  
Old June 3rd, 2011, 07:44 AM
Registered User
 
Join Date: Jun 2011
Posts: 7
Thanks: 1
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by Imar View Post
And what's the error you're getting?

Imar
Error: Invalid attempt to call Read when reader is closed

Its in the thread title. :)
Reply With Quote
  #6 (permalink)  
Old June 3rd, 2011, 07:48 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Vayne (June 3rd, 2011)
  #7 (permalink)  
Old June 3rd, 2011, 08:22 AM
Registered User
 
Join Date: Jun 2011
Posts: 7
Thanks: 1
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by Imar View Post
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..
Reply With Quote
  #8 (permalink)  
Old June 3rd, 2011, 08:48 AM
Registered User
 
Join Date: Jun 2011
Posts: 7
Thanks: 1
Thanked 1 Time in 1 Post
Default

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
Reply With Quote
  #9 (permalink)  
Old June 3rd, 2011, 09:27 AM
Registered User
 
Join Date: Jun 2011
Posts: 7
Thanks: 1
Thanked 1 Time in 1 Post
Default

Woops! My bad, I am actually using a single connection for both, it was global. I have rectified it now. Thanks!
Reply With Quote
  #10 (permalink)  
Old June 3rd, 2011, 09:32 AM
Registered User
 
Join Date: Jun 2011
Posts: 7
Thanks: 1
Thanked 1 Time in 1 Post
Default

*I am just not being insightful*

Last edited by Vayne; June 3rd, 2011 at 11:19 AM.. Reason: Invalid point to make
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Invalid attempt to FieldCount when reader is close hwajunsong Crystal Reports 1 December 28th, 2005 01:18 PM
Invalid attempt to Read when reader is closed. nidy_online VS.NET 2002/2003 0 April 28th, 2004 11:02 AM
Invalid attempt to Read when reader is closed. nidy_online VS.NET 2002/2003 1 April 28th, 2004 02:19 AM
Invalid procedure call Error pavel Pro VB 6 2 January 30th, 2004 10:00 AM



All times are GMT -4. The time now is 12:52 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.