saifullah khan

saifullah khan

  • NA
  • 335
  • 301.1k

registration form

Apr 8 2011 3:30 AM
hello!
  i am creating a registration form, which first identifies if the user is exist or not if not then it add the data of user in database. but it is giving following error.

SqlConnection con = new SqlConnection("Data Source=EMINENCE-PC\\SQLEXPRESS;Initial Catalog=iitkust;Integrated Security=True");
SqlCommand cmd;
SqlDataReader dr;
protected void SignUp_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("select * from Reg where UserName='" + UserName.Text + "' ", con);
dr = cmd.ExecuteReader();
if (dr.Read())
{
Label1.Text = "User exist";
con.Close();
}

else
{
con.Open();
if (!dr.Read())
{

cmd = new SqlCommand("insert into Reg values('" + UserName.Text + "','" + FirstName.Text + "','" + LastName.Text + "','" + UIDNo.Text + "','" + Discipline.Text + "','" + Department.Text + "','" + Email.Text + "','" + Password.Text + "','" + ConfermPassword.Text + "','" + Contact.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
return;



the output gives the following error:
Exception Details: System.InvalidOperationException: The connection was not closed. The connection's current state is open.

Source Error:

Line 30: else
Line 31: {
Line 32: con.Open();
Line 33: if (!dr.Read())
Line 34: {

please tell me the solution

Answers (5)