muntazer
muntazer

Reputation: 1

Invalid attempt to call Read when reader is closed. you can help me

i don't know whats the problem , you can help me to find solution , and fix the problem , the Decrypt() is mathod to decrypt code

var test = Decrypt(cli);
DataTable dt = new DataTable();
conn.Open();
SqlDataReader myReader = null;
SqlCommand id_c = new SqlCommand("Select * From ResearcherInformation where Email='" + test + "'", conn);
myReader = id_c.ExecuteReader();
while (myReader.Read())
    {
        var idid = myReader["Id_Researcher"].ToString();
        conn.Close();
        ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('access');", true);
    }
    myReader.Close();
    conn.Close();

Upvotes: 0

Views: 64

Answers (2)

Rehan Shah
Rehan Shah

Reputation: 1627

You getting this : Error. Invalid attemp to call read. You make a mistake in your code. The Reader object requires a opened connection and you make a mistake that you closed a connection inside a loop that's why you getting this exception.

Sqlconnection connection = new Sqlconnection("//..") // Provide a Correct Connection string.
var test = Decrypt(cli);
DataTable dt = new DataTable();
connection.Open();

// Don't goes for this approach. always try to use Store Procedure instead to this.
SqlCommand command = new SqlCommand("Select * From ResearcherInformation where Email='" + test + "'", connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
   var idid = myReader["Id_Researcher"].ToString();
   // connection.Close(); this is not the a place for closing a connection.
   ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('access');", true);
}
reader.Close();
connection.Close();

Upvotes: 0

David
David

Reputation: 219016

You're closing the connection in the middle of your loop:

while (myReader.Read())
{
    var idid = myReader["Id_Researcher"].ToString();
    conn.Close();
    ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('access');", true);
}

Which is why the connection is closed as soon as you loop over the reader a second time. Don't do that:

while (myReader.Read())
{
    var idid = myReader["Id_Researcher"].ToString();
    ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('access');", true);
}

Close the connection after you use it, not while you use it.


A few additional things worth noting:

  1. Your are relying on SQL-injectable practices. This is both a major security vulnerability and a very common source of bugs. Use query parameters to treat values as values instead of as executable code. This is a good place to start for examples: https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/ado-net-code-examples A Google search for "C# parameterized query" will find more.
  2. Take a look at the use of using statements for things like your SqlConnection object and other objects which implement IDisposable.
  3. You don't use your idid variable for anything, so you can just get rid of it. You also never use dt, so you can get rid of that too.

Upvotes: 1

Related Questions