Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
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
 
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
The Following User Says Thank You to Vayne For This Useful Post:
 
Old June 3rd, 2011, 05:43 AM
Friend of Wrox
 
Join Date: May 2004
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.
 
Old June 3rd, 2011, 05:55 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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!
 
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.
 
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. :)
 
Old June 3rd, 2011, 07:48 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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!
The Following User Says Thank You to Imar For This Useful Post:
Vayne (June 3rd, 2011)
 
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..
 
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
 
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!
 
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.