Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 226865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:33:03+00:00 2026-05-11T19:33:03+00:00

I have a generic method to call a stored Procedure in ASP.NET: public SqlDataReader

  • 0

I have a generic method to call a stored Procedure in ASP.NET:

public SqlDataReader ExecuteStoredProc(string sprocName, SqlParameter[] SqlP)
        {
            SqlDataReader iReader;
            SqlCommand sql = new SqlCommand();

            sql.CommandText = sprocName;
            sql.CommandType = CommandType.StoredProcedure;
            sql.Connection = ConnStr;
            if (SqlP != null)
            {
                foreach (SqlParameter p in SqlP)
                {
                    sql.Parameters.Add(p);
                }

            }
            sql.Connection.Open();
            iReader = sql.ExecuteReader(CommandBehavior.CloseConnection);
            sql.Dispose();

            return iReader;
        }

Even though I am calling CommandBehavior.CloseConnection the connection is not closing. I can get the data fine the first time I request a page. On reload I get the following error:

The connection was not closed. The
connection’s current state is open.
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: The
connection was not closed. The
connection’s current state is open.

Source Error:

Line 35: Line 36: } Line
37: sql.Connection.Open();
Line 38: iReader =
sql.ExecuteReader(CommandBehavior.CloseConnection);
Line 39: sql.Dispose();

Finally if I put sql.Connection.Close(); before sql.Dispose(); I get an error that iReader is not readable because it’s been closed already.

Obviously I am closing my connection incorrectly, can someone point me in the right direction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:33:03+00:00Added an answer on May 11, 2026 at 7:33 pm

    When you return a DataReader, the underlying connection must remain open. It’s the consumer’s responsibility to properly clean up resources.

    public SqlDataReader ExecuteStoredProc(string sprocName, SqlParameter[] SqlP)
    {
        SqlCommand sql = new SqlCommand();
    
        sql.CommandText = sprocName;
        sql.CommandType = CommandType.StoredProcedure;
        sql.Connection = ConnStr;
        if (SqlP != null)
        {
            foreach (SqlParameter p in SqlP)
            {
                sql.Parameters.Add(p);
            }
    
        }
        sql.Connection.Open();
        return sql.ExecuteReader(CommandBehavior.CloseConnection);          
    }
    
    public void ConsumingMethod()
    {
        using(SqlDataReader reader = ExecuteStoredProc("MyProc", params))
        {
            while(reader.Read())
            {
                //work with your reader
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generic method define as below public T MyMethod<T>(extra params) My method
I have two classes: Superclass and derived Subclass:Superclass. I have a generic method: public
I have forgotten the syntax for a generic method: public static void swap <T>
I have a C# string object that contains the code of a generic method,
How can I invoke overloaded generic method in IronRuby? I have a .NET class
I have a generic method: public bool DoSomething<T>(T item) where T: IBase { return
Running .NET 2.0, I have a generic method with the following signature: static listType
I have a generic class that has a method 'Call' on it with a
I have a CRUD-heavy ASP.NET application with all the business logic in Stored Procedures.
Using SubSonic3, I have this generic method (thanks to linq guy, James Curran): public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.