0

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();
    }

}
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Aligator3000
  • 335
  • 3
  • 17
  • have you tried doing a dataGridView.DataBind() after command execution ? – Bala R Feb 25 '11 at 03:09
  • 9
    **DO NOT SAVE A PASSWORD LIKE IN THE DATABASE LIKE THAT!!** This is WRONG. Use [bcrypt](http://stackoverflow.com/questions/873403/net-impl-of-bcrypt) to hash the password, and store the hash in the database. When someone tries to log in, you hash again and compare the hashes. – Joel Coehoorn Feb 25 '11 at 03:10
  • What is the value of mem_id? Is there a reason you're not using a parameter for that as well? – rsbarro Feb 25 '11 at 03:11
  • You're already using parameters in your query - I would **strongly** recommend to also use one for the `mem_id` value! – marc_s Feb 25 '11 at 05:52
  • `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` – safi Feb 25 '11 at 11:10
  • Are you sure you have set mem_id to the right value? – Paddy Feb 25 '11 at 14:24

4 Answers4

1

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 catch to make sure the db con is closed. here you are just using it in the catch, also try to put break point and see if the parameter are passed in correct way and follow it till the end. like

finally
        {
            if (con != null)
            {
                con.Close();
            }
        }
safi
  • 3,636
  • 8
  • 24
  • 37
0

Make sure to put in your PageLoad Clause

** Change Made ** As per @marc_s comment changed if(this.IsPostBack == true) to if(this.IsPostBack) this.IsPostBack is boolean.


if(this.IsPostBack)
{
//dont load page
}
Robbie Tapping
  • 2,516
  • 1
  • 17
  • 18
  • 1
    Actually, since `IsPostBack` already is a boolean, you could also use `if(this.IsPostBack)` and be done with it.... – marc_s Feb 25 '11 at 05:52
0

There are a number of things that could be happening.

  1. 1) The btnUpdateAccount_Click event is never raised. To fix this, make sure the asp:Button tag has an OnClick="btnUpdateAccount_Click" attribute set.
  2. An exception is being thrown and that you're not noticing.
  3. The database is being updated, but you did not load/bind the data again to display the updated values.
Jemes
  • 2,822
  • 21
  • 22
0

You need to bind the data from database again to get the new changes. grid view .bind()