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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:47:54+00:00 2026-05-25T09:47:54+00:00

My code throws the error Invalid attempt to call Read when reader is closed.

  • 0

My code throws the error

Invalid attempt to call Read when reader is closed.

I’m using SqlDataReader to read values from database, and that is my code:

while (rd.Read())
{
    string stateincharge = rd["stateincharge"].ToString();
    string email = rd["email"].ToString();
    cmd.Dispose();
    rd.Close();
    cmd = con.CreateCommand();
    cmd.CommandText = "str_procedure";
    cmd.Parameters.AddWithValue("@stateincharge", stateincharge);
    cmd.CommandType = CommandType.StoredProcedure;
    ad.SelectCommand = cmd;
    ad.Fill(ds);
    count = ds.Tables[0].Rows.Count;
    DataTable dt = new DataTable();
}

This runs in a loop in ASP.NET code-behind.

My problem is that I think I need to close SqlDatReader because of an error shown.

How can I again open sqlDataReader at end of the while loop?

  • 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-25T09:47:55+00:00Added an answer on May 25, 2026 at 9:47 am

    You have to remove the line that closes the reader rd.Close(); because you are closing the reader inside the while loop then you try to access the reader again. Also, I think if you used a new SQL command and new adapter you will not receive this error.

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

Sidebar

Related Questions

The following code throws an compile-time error like Cannot convert type 'string' to 'int'
The following code for a co-worker throws the following error when he tries to
This code does not throw an error but the query fails, that is, the
How can I use JUnit idiomatically to test that some code throws an exception?
gcc lovingly throws me this error: bst.c:33: error: invalid application of ‘sizeof’ to incomplete
I have encountered a strange Invalid Packet Lenght (that is how the error is
I'm using code-first EF 4.1 to insert data into a SQL Server database. When
Consider the following code: std::string my_error_string = Some error message; // ... throw std::runtime_error(std::string(Error:
If my code throws an exception, sometimes - not everytime - the jsf presents
The following code throws System.UriFormatException: var uri = new UriBuilder(ftp://user:pass#word@ftp.somewhere.com:21/fu/bar.zip); System.UriFormatException: Invalid URI: A

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.