473,395 Members | 1,497 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,395 software developers and data experts.

listGroups from SQL server, DataReader allready open

Hi,
I have the following code:
static private SqlConnection thisConnection;
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//

thisConnection = new SqlConnection("Server=BIGBIB; Database=regdb;
Integrated Security=true;");
thisConnection.Open();

listGroups(0);
}

static void listGroups(int id){
SqlCommand thisCommand = new SqlCommand("SELECT * FROM regGroups WHERE
parent = " + id, thisConnection);

SqlDataReader thisReader;

thisReader = thisCommand.ExecuteReader();

while(thisReader.Read()){
Console.WriteLine(" Gruppenavn: {0}, Id: {1}, Added: {2}",
thisReader["name"].ToString(), thisReader["groupId"].ToString(),
thisReader["added"].ToString());
listGroups(Convert.ToInt32(thisReader["groupId"].ToString()));
}
}

I get an Exception, when I run it, that says:
Unhandled Exception: System.InvalidOperationException: There is already
an open
DataReader associated with this Connection which must be closed first.

hmmm, how do I do this? How can I then get all my groups listed???

Hopes theres someone out there that can give me a hint????

thanks
Mikael Syska
Nov 16 '05 #1
1 1141
The problem is probably that you never close the open DataReader. You need
to .Close() the SqlDataReader in listGroups() when you're done using it.
You should also dispose SqlCommand when done, and close and dispose the
SqlConnection.

Thanks,
Michael C., MCDBA

"Mikael Syska" <ne**********@syska.dk> wrote in message
news:eY**************@TK2MSFTNGP10.phx.gbl...
Hi,
I have the following code:
static private SqlConnection thisConnection;
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//

thisConnection = new SqlConnection("Server=BIGBIB; Database=regdb;
Integrated Security=true;");
thisConnection.Open();

listGroups(0);
}

static void listGroups(int id){
SqlCommand thisCommand = new SqlCommand("SELECT * FROM regGroups WHERE
parent = " + id, thisConnection);

SqlDataReader thisReader;

thisReader = thisCommand.ExecuteReader();

while(thisReader.Read()){
Console.WriteLine(" Gruppenavn: {0}, Id: {1}, Added: {2}",
thisReader["name"].ToString(), thisReader["groupId"].ToString(),
thisReader["added"].ToString());
listGroups(Convert.ToInt32(thisReader["groupId"].ToString()));
}
}

I get an Exception, when I run it, that says:
Unhandled Exception: System.InvalidOperationException: There is already
an open
DataReader associated with this Connection which must be closed first.

hmmm, how do I do this? How can I then get all my groups listed???

Hopes theres someone out there that can give me a hint????

thanks
Mikael Syska

Nov 16 '05 #2

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

Similar topics

2
by: martijn | last post by:
Hi There, my vb app can open files. Well, actually my vb app launches word or excell to open them. These files are shared in a network. If somebody else has allready opened that file, you...
2
by: Jake S | last post by:
Hi all, Is it possibele to set the datasource of a dropdown list to a datareader? When I try to the only column I receive is a column populated by the datasource name repeated the amount of...
0
by: Amber | last post by:
Stored procedures are faster and more efficient than in-line SQL statements. In this article we will look at two SQL Server stored procedures; one using an input parameter and one not, and see how...
7
by: [Gauthier] | last post by:
Hello, I've a simple question: On a server that run multiple asp.net HttpApplication, is there any way with the asp.net framework to exchange data between different application? I basically...
20
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
1
by: gerr | last post by:
Visual studio 2003 "HTTP/1.1 500 Internal Server Error" when trying to create a new Asp.net site with Visual Studio 2003 on localServer XP Pro/IIS 5.1. Everything worked fine prior to installing...
10
by: Mike Collins | last post by:
I am trying to connect to a 2003 Server from an ASP.Net page and am getting the following error message. Can someone please help get me started on how to track and fix this error? The code I am...
3
by: Johnny Jörgensen | last post by:
I've got an error that I simply cannot locate: I've got a form in which I use a datareader object to read information from a db. After the read, I close the reader and I dispose of both the...
9
by: fniles | last post by:
I am using VB.NET 2003 and SQL 2005. To use connection pooling and avoid the error "There is already an open DataReader associated with this Connection which must be closed first." , I understand...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.