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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:40:58+00:00 2026-05-26T19:40:58+00:00

I am trying to read from datareader but i am gettin the error Invalid

  • 0

I am trying to read from datareader but i am gettin the error “Invalid attempt to call Read when reader is closed.” The stored procedure is working fine but when i try to read fom datareader it throws error.Plz help me

protected void CheckDatabase()
    {
        SqlConnection conn = new SqlConnection(GetConnectionString());
        conn.Open();
        SqlParameter[] param = new SqlParameter[2];
        param[0]= new SqlParameter("@EmpID", SqlDbType.Int);
        param[0].Value = txtEmpId.Text;
        param[1]= new SqlParameter("@Date", SqlDbType.VarChar,50);
        param[1].Value = txtDate.Text;
        SqlDataReader reader = DNDatabase.ExecuteStoredProcedureReader("RetrieveDeatails", param);
        while (reader.Read())
        {
            gridConfirm.DataSource = reader;
            gridConfirm.Columns[0].Visible = false;
            gridConfirm.DataBind();
                Session["Task_List"] = reader;

        }}

here is stored Procedure code

public static SqlDataReader ExecuteStoredProcedureReader(string procedureName, SqlParameter[] parameters)
    {

        SqlConnection _conn = new SqlConnection(DNDatabase.SQLConnectionString);
        SqlCommand cmd = new SqlCommand(procedureName, _conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = procedureName;
        cmd.CommandTimeout = 300;
        try
        {

            foreach (SqlParameter param in parameters)
            {
                cmd.Parameters.Add(param);
            }
            _conn.Open();

            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (Exception sqlExc)
        {
            throw new Exception("An error occured", sqlExc);
        }
       finally
        {
            if (_conn != null)
                _conn.Close();
        }
    }
  • 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-26T19:40:59+00:00Added an answer on May 26, 2026 at 7:40 pm

    This looks like a bad idea to me:

    Session["Task_List"] = reader;
    

    You execute that and then call reader.Read() again, continuing until there’s nothing left to read. Presumably you also close the reader at some point (I’d hope – ideally with a using statement). Fundamentally, a SqlDataReader is a connected resource – it’s like a stream to the database. It’s not something you should be holding onto for longer than you need to, and it’s certainly not something you should be putting in a session, even if you didn’t effectively invalidate it with the subsequent Read() call.

    I assume that data binding works by fetching the data when you call DataBind(), but there’s no indication of what you’re trying to achieve by putting a reference to the reader itself into the session.

    I suggest that:

    • You isolate whether this is actually to do with the session or the databinding
    • If it’s the session, you pull all of the data you need out (e.g. into a DataTable or some other “disconnected” form)
    • If it’s the databinding, you’ll need to do something similar – but as I say, you can hope that that’s not the problem, to start with – I assume you saw this sort of databinding elsewhere?

    Additionally, you should consider why you want to loop – currently you’re basically going to loop over all the results and only the last one is going to be displayed (because you’re overwriting the databinding each time). Is that really what you want?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read from a file, but the only thing I get on
I read data from MS Access using C#. But get the OleDbException trying to
I'm trying to read values from the registry but somehow I seem to be
I am trying to read from a blocking socket, but I am wondering that
I am trying to read from a config file in ProgramFiles/MyApp but in Windows
I am trying to read from file: The file is multiline and basically i
I'm trying to read variables from a batch file for later use in the
I'm trying to read data from a.csv file to ouput it on a webpage
I am trying to read data from excel files using datatable. The command select
I am trying to read values from an input file in Perl. Input file

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.