Reputation: 1
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
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
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:
using
statements for things like your SqlConnection
object and other objects which implement IDisposable
.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