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
Loading
Karthikeyan AnbarasanPosted Apr 8, 2011, 5:35 AM
saifullah khanPosted Apr 8, 2011, 5:23 AM
piyush sardharaPosted Apr 8, 2011, 5:10 AM
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();
}
frist check condition of that
in that code if dr["userName"]==txtusername
then you write
Label1.Text = "User exist";
saifullah khanPosted Apr 8, 2011, 3:47 AM
piyush sardharaPosted Apr 8, 2011, 3:38 AM