473,769 Members | 5,072 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Close connection problem

Hello there people,

I'm having some kind of problem. I have a function that returns a
datareader. At some point using the application I get an error
"Unspecifie d error" (ssssoooo helpful) :). I think I know the problem.
My Connection remains open.

Is there a way I can do to close the Connection. below is the code.
Thank you very much as always

public System.Data.Ole Db.OleDbDataRea der DataReaderFunc( string
SQLSelect)
{
OleDbDataReader objDataRead = null;
OleDbConnection objConn = new OleDbConnection ();

objConn.Close() ;
objConn.Connect ionString = GetConnection() ;
OleDbCommand objComm = new OleDbCommand(SQ LSelect, objConn);
objConn.Open();
objDataRead = objComm.Execute Reader(CommandB ehavior.CloseCo nnection);

Console.WriteLi ne (objConn.State) ;
//objConn.Dispose ();
return objDataRead;
}

Dec 29 '05 #1
5 2059
Your Connection remains open, alright. Worse than that, its scoped inside a
method, so when your method exits, you lose any handle on it you had while
the method was executing.

A DataReader requires a persistent Connection. For the entire time you use a
DataReader, its Connection must remain open. Therefore, you need to declare
your Connection outside of the method, so that when you are finished with
the DataReader, you can close the Connection.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"Varangian" <of****@gmail.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Hello there people,

I'm having some kind of problem. I have a function that returns a
datareader. At some point using the application I get an error
"Unspecifie d error" (ssssoooo helpful) :). I think I know the problem.
My Connection remains open.

Is there a way I can do to close the Connection. below is the code.
Thank you very much as always

public System.Data.Ole Db.OleDbDataRea der DataReaderFunc( string
SQLSelect)
{
OleDbDataReader objDataRead = null;
OleDbConnection objConn = new OleDbConnection ();

objConn.Close() ;
objConn.Connect ionString = GetConnection() ;
OleDbCommand objComm = new OleDbCommand(SQ LSelect, objConn);
objConn.Open();
objDataRead = objComm.Execute Reader(CommandB ehavior.CloseCo nnection);

Console.WriteLi ne (objConn.State) ;
//objConn.Dispose ();
return objDataRead;
}

Dec 29 '05 #2
He is passing in ConnectionBehav ior.CloseConnec tion. So as long as the data
reader is closed, so will the connection, and that will avoid a connection
leak. This means that the connection is fine declared where it is.

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:es******** *****@TK2MSFTNG P14.phx.gbl...
Your Connection remains open, alright. Worse than that, its scoped inside
a method, so when your method exits, you lose any handle on it you had
while the method was executing.

A DataReader requires a persistent Connection. For the entire time you use
a DataReader, its Connection must remain open. Therefore, you need to
declare your Connection outside of the method, so that when you are
finished with the DataReader, you can close the Connection.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"Varangian" <of****@gmail.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Hello there people,

I'm having some kind of problem. I have a function that returns a
datareader. At some point using the application I get an error
"Unspecifie d error" (ssssoooo helpful) :). I think I know the problem.
My Connection remains open.

Is there a way I can do to close the Connection. below is the code.
Thank you very much as always

public System.Data.Ole Db.OleDbDataRea der DataReaderFunc( string
SQLSelect)
{
OleDbDataReader objDataRead = null;
OleDbConnection objConn = new OleDbConnection ();

objConn.Close() ;
objConn.Connect ionString = GetConnection() ;
OleDbCommand objComm = new OleDbCommand(SQ LSelect, objConn);
objConn.Open();
objDataRead = objComm.Execute Reader(CommandB ehavior.CloseCo nnection);

Console.WriteLi ne (objConn.State) ;
//objConn.Dispose ();
return objDataRead;
}


Dec 29 '05 #3
Ah, yes. Sorry, overlooked it.

Well, the only thing left is whether or not he's closing the DataReader
outside the method somewhere.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"Marina" <so*****@nospam .com> wrote in message
news:uu******** *****@TK2MSFTNG P10.phx.gbl...
He is passing in ConnectionBehav ior.CloseConnec tion. So as long as the
data reader is closed, so will the connection, and that will avoid a
connection leak. This means that the connection is fine declared where it
is.

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:es******** *****@TK2MSFTNG P14.phx.gbl...
Your Connection remains open, alright. Worse than that, its scoped inside
a method, so when your method exits, you lose any handle on it you had
while the method was executing.

A DataReader requires a persistent Connection. For the entire time you
use a DataReader, its Connection must remain open. Therefore, you need to
declare your Connection outside of the method, so that when you are
finished with the DataReader, you can close the Connection.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"Varangian" <of****@gmail.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Hello there people,

I'm having some kind of problem. I have a function that returns a
datareader. At some point using the application I get an error
"Unspecifie d error" (ssssoooo helpful) :). I think I know the problem.
My Connection remains open.

Is there a way I can do to close the Connection. below is the code.
Thank you very much as always

public System.Data.Ole Db.OleDbDataRea der DataReaderFunc( string
SQLSelect)
{
OleDbDataReader objDataRead = null;
OleDbConnection objConn = new OleDbConnection ();

objConn.Close() ;
objConn.Connect ionString = GetConnection() ;
OleDbCommand objComm = new OleDbCommand(SQ LSelect, objConn);
objConn.Open();
objDataRead = objComm.Execute Reader(CommandB ehavior.CloseCo nnection);

Console.WriteLi ne (objConn.State) ;
//objConn.Dispose ();
return objDataRead;
}



Dec 29 '05 #4
In my experience, when there is a connection leak and there is a problem
obtaining a new connection, the error message is usually something that
mentions timing out getting a new connection from the connection pool.

Also, database errors will usually have more information other then
'unspecified error'. Are you sure this is the complete error message in the
exception being thrown?

To avoid connection leaks in this type of code, you should make sure you
close the datareader in whatever code is calling this function, to make sure
that the connection the data reader is using is closed.

"Varangian" <of****@gmail.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Hello there people,

I'm having some kind of problem. I have a function that returns a
datareader. At some point using the application I get an error
"Unspecifie d error" (ssssoooo helpful) :). I think I know the problem.
My Connection remains open.

Is there a way I can do to close the Connection. below is the code.
Thank you very much as always

public System.Data.Ole Db.OleDbDataRea der DataReaderFunc( string
SQLSelect)
{
OleDbDataReader objDataRead = null;
OleDbConnection objConn = new OleDbConnection ();

objConn.Close() ;
objConn.Connect ionString = GetConnection() ;
OleDbCommand objComm = new OleDbCommand(SQ LSelect, objConn);
objConn.Open();
objDataRead = objComm.Execute Reader(CommandB ehavior.CloseCo nnection);

Console.WriteLi ne (objConn.State) ;
//objConn.Dispose ();
return objDataRead;
}

Dec 29 '05 #5
Unspecified error
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.Data.Ole Db.OleDbExcepti on: Unspecified error
-

Line 54: OleDbCommand objComm = new OleDbCommand(SQ LSelect,
objConn);
Line 55:
Line 56: objConn.Open();
Line 57: objDataRead =
objComm.Execute Reader(System.D ata.CommandBeha vior.CloseConne ction);

This is the error I'm getting. with the
ExecuteFunction (CloseConnectio n) the application lasts a bit more..
however there's still something wrong and the error points into the
objConn.Open();

:) Thanks for the Help

Dec 30 '05 #6

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

Similar topics

3
373
by: sam | last post by:
Hello group, I have a function which is used to initiate sqlDataReader object. I was trying to invoke the close method on the DataReader object but cant really do that as the function returns a datareader and cannot access the datareader once the connection is closed. Here is what I do: public function getDataReader() as datareader
3
9114
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on the client machine to close per my network app the computer fills up w/ thousands of these :0 TCP foobox:8888 localhost:2188 TIME_WAIT :0 TCP foobox:8888 localhost:2189 TIME_WAIT :0 TCP foobox:8888 localhost:2190 TIME_WAIT
6
4605
by: No_Excuses | last post by:
All, I am interested in reading the text of a web page and parsing it. After searching on this newgroup I decided to use the following: ******************************* START OF CODE ************************ String sTemp = "http://cgi3.igl.net/cgi-bin/ladder/teamsql/team_view.cgi?ladd=teamknights&num=238&showall=1"; WebRequest myWebRequest = WebRequest.Create(sTemp); WebResponse myWebResponse = myWebRequest.GetResponse();
4
18135
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed as I find appropriate. I am using the socket asynchronously by calling the BeingSend and BeginReceive calls. I would like to be able to call shutdown and close asynchronously if possible.
8
2162
by: Pierson C | last post by:
I am developing on a website that is utilizing SQL Server 2000. Shortly after deploying the site, we began having timeout issues due to the max connections. 1st instinct was to diligently tidy up our connections. We did so by encapsulating with the using statement. Ex: using (SqlConnection myConnection = new SqlConnectionConfigurationSettings.AppSettings))
35
11421
by: Eric Sabine | last post by:
In my Finally block, I was using cn.close (where cn is an ADO.NET connection object, SQLConnection to be exact) and then I came across the following in some microsoft code. If Not cn Is Nothing Then CType(cn, IDisposable).Dispose() End If I have to admit, I'm not sure what happens here. Will someone explain this line of code (the middle one, not the if statement LOL) to me please?
0
3908
by: bonita | last post by:
In my ASP.NET page, I have 2 checkboxes for users to choose which crystal report they want to display. These two reports use different tables. If report1 has been choosen and displayed in the crystal report, then I cannot check another checkbox to display report2 afterwards. If I close the website and open again, I can choose report2 and display as crystal report correct, but now I cannot check the checkbox1 to display report 1 afterwards....
17
2384
by: Alan Silver | last post by:
Hello, I have a generic method in a utility class that grabs an sqldatareader and returns it. Due to the fact that (AFAIK), you can't close the database connection before you've read the data, this method doesn't close it, it just returns the datareader. The calling code uses the datareader and then just lets it drop out of scope, to be picked up by the garbage collector. Is this a problem? A friend of mine suggested to me that not...
8
9637
by: Greg Strong | last post by:
Hello All, The short questions are 1 Do you know how to make DSN connection close in Access to Oracle 10g Express Edition? &/or 2 Do you know how to make a DSN-less pass-through query work from
0
10214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9996
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.