Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > VS.NET 2002/2003
|
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 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 December 16th, 2003, 03:57 AM
kaz kaz is offline
Authorized User
 
Join Date: Dec 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default "There is already an open DataReader " error

I got an error message, "There is already an open DataReader associated with this Connection 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 Connection which must be closed first."
What did I do wrong? Here is the part of the code.

string searchProjectSQL="SELECT ProjectID, ProjectName, DateOfEvent FROM Project WHERE ProjectName ='" + ProjectName +"' AND DateOfEvent = '" +DateEvent+ "'";
Int32 intProjectID;
OleDbCommand searchProjectCommand = new OleDbCommand(searchProjectSQL,myConnection);
                ProjectDataReader = searchProjectCommand.ExecuteReader();

                if(ProjectDataReader.Read()==true)
                {
                    intProjectID = Convert.ToInt32(ProjectDataReader["ProjectID"].ToString());

                    string foreignRegiProjectSQL = "UPDATE Registration SET ProjectID ="+intProjectID+" ";
                    foreignRegiProjectSQL += @"WHERE (ProjectID IN (SELECT ProjectID FROM Project ";
                    foreignRegiProjectSQL += @" WHERE ProjectName='" +ProjectName+ "' AND ";
                    foreignRegiProjectSQL += @"DateOfEvent='"+DateEvent+"'))";

                    string UserProjectPSQL = "UPDATE UserProject SET UserID="+UserID+", ";
                    UserProjectPSQL += @"ProjectID =" +intProjectID+ ", CreateDate='"+DateTime.Now.ToString()+"' ";
                    UserProjectPSQL += @"WHERE (ProjectID IN (SELECT ProjectID FROM Project ";
                    UserProjectPSQL += @"WHERE ProjectName='"+ProjectName+"' AND ";
                    UserProjectPSQL += @"DateOfEvent='"+DateEvent+"'))";

                    OleDbCommand foreignRegiProjectCommand = new OleDbCommand(foreignRegiProjectSQL,myConnection);
                    OleDbCommand UserProjectPCommand = new OleDbCommand(UserProjectPSQL,myConnection);
                    foreignRegiProjectCommand.ExecuteNonQuery(); UserProjectPCommand.ExecuteNonQuery();
                }
                ProjectDataReader.Close();

Thanks,

 
Old December 16th, 2003, 08:58 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Once you are done with your initial query, you need to close the reader. It looks like you are using the reader only to get one value so close it right after that.

intProjectID = Convert.ToInt32(ProjectDataReader["ProjectID"].ToString());
ProjectDataReader.Close();
//proceed with code and your following NonQuery won't fail.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old December 16th, 2003, 08:00 PM
kaz kaz is offline
Authorized User
 
Join Date: Dec 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How about this case? If I put on a couple of ExecuteReaders sequentially, they will fail. For example,
OleDbCommand searchProjectCommand = new OleDbCommand(searchProjectSQL,myConnection);
                OleDbDataReader ProjectDataReader = searchProjectCommand.ExecuteReader();

                OleDbCommand searchLocationCommand = new OleDbCommand(searchLocationSQL,myConnection);
                OleDbDataReader LocationDataReader = searchLocationCommand.ExecuteReader();


                if(ProjectDataReader.Read()==true) && LocationDataReader.Read() == true)
{
..........

 
Old December 17th, 2003, 07:10 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Sure they'll fail, because you can only have one datareader open at a time. What's your point? Are you asking how to do this? How to open two data set's at the same time? The answer will most likely be to use a DataSet. By using a dataset, you can create multiple datatables that are disconnected instead of a single connect datareader.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old December 17th, 2003, 11:43 PM
kaz kaz is offline
Authorized User
 
Join Date: Dec 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks!!!






Similar Threads
Thread Thread Starter Forum Replies Last Post
DataReader error on Listing 12-19 cJeffreywang BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 0 January 3rd, 2008 12:36 PM
There is already an open datareader associated wit yasinirshad ADO.NET 1 August 12th, 2007 03:37 AM
There is already an open DataReader associated wit jayanp ADO.NET 1 July 2nd, 2006 01:10 PM
There is already an open DataReader associated wit flyin ADO.NET 14 February 20th, 2004 08:41 AM





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