Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am using the following code for update.
when I try to change the password, I get alert message but updated in my database...
What do I have to do?
C#
protected void Button1_Click(object sender, EventArgs e)
{
  con.Open();
  string qry = "select uname,pass from usertab";
  SqlCommand cmd = new SqlCommand(qry, con);
  SqlDataReader dr = cmd.ExecuteReader();
  while (dr.Read())
  {
    if (lbuser.Text.Equals(dr[0].ToString()) && (TextBox1.Text.Equals(dr[1].ToString())))
    {
      SqlCommand cmd1 = new SqlCommand("update usertab set pass='" + TextBox2.Text  +"' where uname='"+ lbuser.Text + "' ",con );
                
      //cmd1.ExecuteNonQuery();
      Response.Write("<script>alert('Password Changed')</script>");                
    }
    else
    {
      Label5.Visible = true;
    }
  }
}
Posted
Updated 17-Nov-11 3:02am
v2
Comments
Mehdi Gholam 17-Nov-11 9:06am    
Why are you going through the entire table just to update a single user, the update command and the where clause will do that perfectly fine.
Raghupathiraja 17-Nov-11 9:09am    
can u give an idea to do this process...
i want to change the user's password in my project?
can u help me?

You do realize you commented out the bit which actually updates the database, don't you?
 
Share this answer
 
Comments
Dylan Morley 17-Nov-11 9:08am    
haha!
Raghupathiraja 17-Nov-11 9:12am    
wt haha!!!!
write the query but execute that query
cmd.ExecuteNonQuery()
 
Share this answer
 
Comments
Raghupathiraja 17-Nov-11 9:40am    
which query
Raghupathiraja 17-Nov-11 9:42am    
There is already an open DataReader associated with this Command which must be closed first.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.

Source Error:


Line 35: {
Line 36: SqlCommand cmd1 = new SqlCommand("update usertab set pass='" + TextBox2.Text + "' where uname='" + lbuser.Text + "' ", con);
Line 37: cmd1.ExecuteNonQuery();
Line 38: Response.Write("<script>alert('Password Changed')</script>");
Line 39: }

i got this error
i solved this prob..
code is here...

protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
string qry = "select uname,pass from usertab";
SqlCommand cmd = new SqlCommand(qry, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (lbuser.Text.Equals(dr[0].ToString()) && (TextBox1.Text.Equals(dr[1].ToString())))
{
SqlConnection con1 = new SqlConnection("Data Source=RAGHU-PC\\SQLExpress;Initial Catalog=cache;User ID=sa;password=sa");
con1.Open();
SqlCommand cmd1 = new SqlCommand("update usertab set pass='" + TextBox2.Text + "' where uname='" + lbuser.Text + "' ", con1);
//dr.Close();
cmd1.ExecuteNonQuery();
// dr.Close();
Response.Write("<script>alert('Password Changed')</script>");
}
else
{
Label5.Visible = true;
}

}


}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900