1

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?

3
  • Can you explain what you're trying to do? I think you're understanding is mixed up. And if you could please explain maybe we could point out a better way of doing it. Commented Aug 29, 2011 at 8:47
  • at which line do you receive 'close DataReader first error'? Commented Aug 29, 2011 at 8:48
  • 1
    Can you put the code that whole code because i think you are doing something wrong with the variables Commented Aug 29, 2011 at 8:53

4 Answers 4

2
// connection for reader
using (SqlConnection connection1 = new SqlConnection(connectionString))
using (SqlCommand command1 = new connection1.CreateCommand())
{
    command1.CommandText = commandText1;

    connection1.Open();
    using (SqlDataReader reader = command1.ExecuteReader())
    {
        // fill table in loop
        while (reader.Read())
        {
            string stateincharge = reader["stateincharge"].ToString();
            string email = reader["email"].ToString();

            // connection for adapter
            using (SqlConnection connection2 = new SqlConnection(connectionString))
            using (SqlCommand command2 = new connection2.CreateCommand())
            {
                command2.CommandText = commandText2;

                command2.Parameters.AddWithValue("@stateincharge", stateincharge);
                command2.Parameters.AddWithValue("@email ", email );

                connection2.Open();

                DataTable table = new DataTable();
                using (SqlDataApapter adapter = new SqlDataAdapter(command2))
                {
                    adapter.Fill(table);
                    // yield return table;
                }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

using new connection for adapter
0

Each connection can have at most one active command (which includes the "response", that is, the reader). You're trying to do a nested call to the DB and this requires that you either load the data completely (for instance into a DataTable) before looping, or that you uise a second connection for the nested calls to str_procedure.

Comments

0
while (rd.Read())
    {
        string stateincharge = rd["stateincharge"].ToString();
        string email = rd["email"].ToString();
        cmd.Dispose();
        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();
}
rd.Close();

1 Comment

There is already an open DataReader associated with this Command which must be closed first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.