Re: Data Reader Error Please Help its Urgent

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: DalePres (don-t-spa-m-me_at_lea-ve-me-a-lone--.com)
Date: 02/04/05


Date: Fri, 4 Feb 2005 09:37:25 -0600

I am just guessing here but probably the DataSet.Fill method calls a
DataReader to do the work.

There are a few possibilities that come to my mind. First, you might create
a non-static method or class so that each user has their own instance rather
than a shared instance. If you must use a static instance, you might try
using a lock statement to block others until the first user is finished with
the code section.

You could wrap your functionality in a using statement, such as
SqlConnection sqlConn = new SqlConnection()

{

}

This makes sure that the connection object is disposed when the using
section is exited.

Also, when I test for connection state, I test with if (conn.ConnectionState
!= ConnectionState.Closed). There are other conditions the ConnectionState
can be other than Open that are still not Closed.

Hope this helps,

DalePres
MCAD, MCDBA, MCSE

"Amit D.Shinde" <amit_sd01@yahoo.com> wrote in message
news:9aee070d.0502040726.302f448b@posting.google.com...
> Hello
>
> I have on clsGlobal.cs File in which i have created many functions of
> Fetching, Inserting Data In Database.. Using Ado.net AsP.net C#.net
> Those Functions returns String, Dataset etc.
> But NO WHERE I AM USING DATAREADER IN WHOLE PROJECT.
>
> But i Get the Error While at a time two users logs in i.e. more than
> one user try to access my webapplication.
>
> The error is as follows:
>
> "There is already an open DataReader associated with this Connection
> which must be closed first. "
> I know why this error occurs .. .due to not closing of connection but
> everywhere i am closing the connection ..
>
> Please Help Me i am sending some samples where error occurss..
>
>
> public static DataSet ExecuteSPDS(SqlCommand pCmd)
> {
> DataSet sResult;
> try
> {
> SqlDataAdapter da = new SqlDataAdapter(pCmd);
> DataSet ds = new DataSet();
> da.Fill(ds,"RECORD");
> sResult = ds;
> }
> catch(System.Exception ex)
> {
> sResult = null;
>
> }
> return sResult;
> }
>
>
>
> ========================================================
> public static string ExecuteScalarQuery1(string query)
> {
> string Result;
> //try
> {
> SqlCommand cmd = new SqlCommand(query,m_Connection);
> if(m_Connection.State==ConnectionState.Closed)
> {
> m_Connection.Open();
> }
> Result=Convert.ToString(cmd.ExecuteScalar());
> if(m_Connection.State==ConnectionState.Open)
> {
> m_Connection.Close();
> }
> //catch(System.Exception ex)
> {
> }
> return Result;
> }
> }
> ====================================================



Relevant Pages

  • Re: ASP.NET architecture : am I missing something
    ... > Make sure to Closeyour DataReader:) ... >> I now open and close the connection before each database manipulation, ... So I looked that concept up for Delphi and it turns out it ... >>> functionality to create and open an SqlConnection and close it ASAP ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Connection string information
    ... I just decompiled the SQLConnection class. ... > private string _password; ... If an element exists within the connection string it parses it, ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Close not closing...
    ... string connectionString; ... // create a new SqlConnection object with the appropriate ... connection string SqlConnection sqlConn = new ... If I hit the script button first, ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Connection string information
    ... I just decompiled the SQLConnection class. ... private string _password; ... inherited from DBConnectionString but my decompiler was not good enough to ... If an element exists within the connection string it parses it, ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: datareader - connection leaks in DB
    ... having a function return a datareader is a bad idea, ... leads to connection leaks, as you have found. ... | Public Function Login(ByVal email As String, ... | Dim myConnection As New ...
    (microsoft.public.dotnet.framework.aspnet)