473,322 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Odd exception that doesn't make sense to me

I am getting this exception when running my code:

{"There is already an open DataReader associated with this Command which
must be closed first."}

This is my code:
private Guid CreateCode( string Code )
{
Guid toReturn = Guid.Empty;
this.scCreateCode.Parameters["@Code"].Size = Code.Length;
this.scCreateCode.Parameters["@Code"].Value = Code;
try
{
this.scCreateCode.ExecuteNonQuery();
toReturn = ( Guid )this.scCreateCode.Parameters["@ID"].Value;
}
catch ( Exception ex )
{ //Breakpoint here

}
return toReturn;
}

The point of the failure is obviously:
this.scCreateCode.ExecuteNonQuery();

This isn't returning a DataReader, or using one as far as I know. What is
causing this exception is the calling function which has this code in it:
try
{
results = this.scCheckCode.ExecuteReader();

if ( !results.HasRows )
{
//Calls it here
toReturn = this.CreateCode( Code );
}
else
{
results.Read();
toReturn = ( Guid )results["CodeID"];
}
}

Now why does it matter if I close the DataReader for a seperate SqlCommand
object before running another? Is there a reason for this? Once I change the
code to:
if ( !results.HasRows )
{
results.Close();
results.Dispose();
results = null;
toReturn = this.CreateCode( Code );
}

It works just fine. Both SqCommand objects use the same SqlConnection
object. I am lacking the knowledge for why this works out the way it does,
could someone please shed some light on this?

Thanks,
Chris
Feb 23 '06 #1
1 965
Hi Chris,

This is a typical exception thrown when trying to do something on a
connection, on which a DataReader has already opened. in .NET 1.1, it
doesn't support to open multiple active result sets on the same opened
connection. You have to close the DataReader first, and then execute the
stored procedure on the same connection. Or you can try to open another
connection object.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Feb 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Aggelos I. Orfanakos | last post by:
Hello. In a program, I want to ensure that a socket closes (so I use try ... finally), but I also want to catch/handle a socket exception. This is what I have done: try: try: s = ... #...
21
by: Stephan | last post by:
why does the following code not work???? after compiling and running it will just say killed after all my memory filled up any suggestions? #include <iostream> using namespace std; void...
7
by: Douglas Peterson | last post by:
Take a look at this code, it looks funny as its written to be as short as possible: -- code -- struct Base { ~Base() { *((char*)0) = 0; } }; struct Derived : public Base
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
15
by: Ant | last post by:
Hi, This might seem like a strange question but I'm wondering how other developers go about choosing the appropriate Exception objects to use in their catch statements. Currently, I choose them...
3
by: Nick | last post by:
Hi there, This probably wont make much sense but I'm getting an unhandled exception being thrown in the following line... Try While True Redraw control as necessary End While Catch ex as...
3
by: JohnDeHope3 | last post by:
First let me say that I understand that Asp.Net wraps my exception in an HttpUnhandledException. I have found a lot of discussion about that on the web, which was informative, but not helpful. Let...
7
by: Sek | last post by:
Hi Folks! I was pondering over a code and noticed that exception handlers were present in the private, protected as well as public methods. And, ofcourse, public methods were calling priv/prot...
3
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own...
35
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.