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 8412449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:40:10+00:00 2026-06-10T00:40:10+00:00

i have created a function which executes query and returns SqlDataReader, now i am

  • 0

i have created a function which executes query and returns SqlDataReader, now i am using that in another function work with the returned data, but i gets the error saying reader is already closed. here is the functions:

public static SqlDataReader ExecuteReader(string procedure, SqlParameter[] parameters, CommandType commandType)
    {
        SqlDataReader reader = null;
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlCommand command = new SqlCommand(procedure, connection))
            {
                connection.Open();
                if(parameters != null)
                {
                    if (commandType == CommandType.StoredProcedure)
                        command.Parameters.AddRange(parameters);
                }
                reader = command.ExecuteReader();
            }
        }
        return reader;
    }

here is the code where i am calling the SqlDataReader

using (SqlDataReader reader = SqlHelper.ExecuteReader("select top 10 username from users", null, System.Data.CommandType.Text))
        {
            Response.Write(reader.IsClosed); //This returns True

        }
  • 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-06-10T00:40:12+00:00Added an answer on June 10, 2026 at 12:40 am

    EDIT

    ExecuteReader with CommanBehavior ( automatically close connection after reading data)

    To over come connection closing proble just make use of CommandBheviour

    - CommandBehavior.CloseConnection 
       When you pass above values as argument to ExecuteReader 
         1. there is no need to close connection explicitly connection get close when you close your reader. 
    

    code will be like this no need to close connection explicitly

    public void CreateMySqlDataReader(string mySelectQuery,string myConnectionString) 
    {
       SqlConnection myConnection = new SqlConnection(myConnectionString);
       SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
       myConnection.Open();
       SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
       while(myReader.Read()) 
       {
          Console.WriteLine(myReader.GetString(0));
       }
       myReader.Close();
       //Implicitly closes the connection because CommandBehavior.CloseConnection was specified.
    }
    

    its causing problem because you are closing connection

    SqlReader always make use of open connection i.e live connection which is open when you use this

    using (SqlConnection connection = new SqlConnection(connectionString)) 
            {
    }
    

    it dispose connection object which is used by reader object that why its returing IsColosed as true

    If you wanto return value the objec than make use of DataTable which is disconnected data object and doens makse use of connection

    Modified code

      public static DataTable ExecuteReader(string procedure, SqlParameter[] parameters, CommandType commandType)
      {
         DataTable dt = null;
         using (SqlConnection connection = new SqlConnection(connectionString))
         {
             using (SqlCommand command = new SqlCommand(procedure, connection))
             {
                  connection.Open();
                  if(parameters != null)
                  {
                if (commandType == CommandType.StoredProcedure)
                command.Parameters.AddRange(parameters);
                  }
                    SqlDataAdapter da = new SqlDataAdapter(command);
                    da.Fill(dt);
               }
             }
                return dt;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using xlwDotNet, I have created a simple function which returns one number. Sometimes, something
I have created a JS function which executes fine when it's included an the
I have created a pipelined function which returns a table. I use this function
I have created a function which gets an encoded string (possibly UTF-16 not sure)
I have created an utility function which I include in almost all Javascript code
I have created a add input field function which is working fine. I would
I have the following function which creates a std::vector of iterators into another container:
I have an PostgreSQL Function which returns me an XML where the whole infos
I have created a function in F# to recover historical data from Yahoo (the
I have a query that uses a lot of aggregate functions to build data

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.