I am using the follwing code to update a person's email and password in the db. I have a datagridview which has only one row. When I hit the Update button, nothing happens - the page is refreshed and the values in the textboxes go back to what they were before....the update is not working. Please help. Thanks!
protected void btnUpdateAccount_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
string sql = "UPDATE Member SET [Email] = @email, [Password] = @password WHERE [MemberID] = '" + mem_id + "'";
TextBox email = email = (TextBox)Gridview1.Rows[0].FindControl("user_email");
TextBox password = (TextBox)Gridview1.Rows[0].FindControl("user_password");
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@email", SqlDbType.VarChar);
cmd.Parameters.Add("@password", SqlDbType.VarChar);
cmd.Parameters["@email"].Value = email.Text;
cmd.Parameters["@password"].Value = password.Text;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error: ";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
mem_idvalue!cmd.ExecuteNonQuery(); it return an integer value, so you can put. int i = cmd.ExecuteNonQuery(); and see what it is returning, also you can use finally after try to make sure the db con is closed. here you are just using it in the catch which isnt good