locked
sql update statement is not work.. RRS feed

  • Question

  • User-721945135 posted

    hi,  i'm using asp.net 3.5 c# with access database, i have update problem in my web application. After i click on update button, the data remain the same and not error page or message is show. I dont know where i wrote wrongly, please help me to see where i wrote wrongly. Thanks.

    My codes are look like this:

    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            if (Page.IsValid)
            {
                string sql = "UPDATE Member" + 
                             "SET fullname=?, gender=?, DOB=?, address=?, hp=?, email=?, securityquestion=?, securityanswer=?" + 
                             "WHERE username=?";
                OleDbConnection con = new OleDbConnection(cs);
                OleDbCommand cmd = new OleDbCommand(sql, con);
                cmd.Parameters.AddWithValue("", txtFullname.Text);
                cmd.Parameters.AddWithValue("", rblGender.SelectedValue);
                cmd.Parameters.AddWithValue("", txtBirthday.Text);
                cmd.Parameters.AddWithValue("", txtAddress.Text);
                cmd.Parameters.AddWithValue("", txtHp.Text);
                cmd.Parameters.AddWithValue("", txtEmail.Text);
                cmd.Parameters.AddWithValue("", ddlSecurity.SelectedValue);
                cmd.Parameters.AddWithValue("", txtAnswer.Text);
                cmd.Parameters.AddWithValue("", lblUsername.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }

    protected void btnUpdate_Click(object sender, ImageClickEventArgs e)

        {

            if (Page.IsValid)

            {

                string sql = "UPDATE Member" + 

                             "SET fullname=?, gender=?, DOB=?, address=?, hp=?, email=?, question=?, answer=?" + 

                             "WHERE username=?";


                OleDbConnection con = new OleDbConnection(cs);

                OleDbCommand cmd = new OleDbCommand(sql, con);


                cmd.Parameters.AddWithValue("", txtFullname.Text);

                cmd.Parameters.AddWithValue("", rblGender.SelectedValue);

                cmd.Parameters.AddWithValue("", txtBirthday.Text);

                cmd.Parameters.AddWithValue("", txtAddress.Text);

                cmd.Parameters.AddWithValue("", txtHp.Text);

                cmd.Parameters.AddWithValue("", txtEmail.Text);

                cmd.Parameters.AddWithValue("", ddlQuestion.SelectedValue);

                cmd.Parameters.AddWithValue("", txtAnswer.Text);

                cmd.Parameters.AddWithValue("", lblUsername.Text);


                con.Open();

                cmd.ExecuteNonQuery();

                con.Close();

            }

        }


    Friday, September 17, 2010 5:49 AM

Answers

  • User-721945135 posted

    hans v, 
    I know what cause my update button not function, it is a codes that used to display user data during page load under "protected void Page_Load(object sender, EventArgs e)" then i add a code like "if (!Page.IsPostBack)" and i put all those codes inside it.
    Now my update button is work.

    hans v, 

    I know what cause my update button not function, it is a codes that used to display user data during page load under "protected void Page_Load(object sender, EventArgs e)" then i add a code like "if (!Page.IsPostBack)" and i put all those codes inside it.

    Now my update button is work.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 20, 2010 1:39 AM

All replies

  • User-1802908944 posted

    where is parameter 

      string sql = "UPDATE Member" + 

                             "SET fullname=@FullName, gender=@Gender, DOB=@DOB, address=@address, hp=@hp, email=@email, question=?, answer=@answer" + 

                             "WHERE username=@Username";


                OleDbConnection con = new OleDbConnection(cs);

                OleDbCommand cmd = new OleDbCommand(sql, con);

                cmd.Parameters.AddWithValue("@FullName", txtFullname.Text);

                cmd.Parameters.AddWithValue("@Gender", rblGender.SelectedValue);

    so on.......... it will work

    Friday, September 17, 2010 6:07 AM
  • User1283497924 posted

    Hi,

    Please try this...

    protected void btnUpdate_Click(object sender, ImageClickEventArgs e)

        {

            if (Page.IsValid)

            {

     string sql = "UPDATE [Member] SET [fullname]='"+txtFullname.Text+"', [gender]='"+rblGender.SelectedValue+"', [DOB]='"+txtBirthday.Text+"', [address]='"+txtAddress.Text+"', [hp]='"+txtHp.Text+"', [email]='"+txtEmail.Text+"', [question]='"+ddlQuestion.SelectedValue.Tostring()+"', [answer]='"+txtAnswer.Text+"' WHERE [username]='"+blUsername.Text+"'";

    OleDbConnection con = new OleDbConnection(cs);

    OleDbCommand cmd = new OleDbCommand(sql, con);

     con.Open();

                cmd.ExecuteNonQuery();

                con.Close();

            }

        }



    Friday, September 17, 2010 6:20 AM
  • User888410735 posted

    why are u using ? when you wanna update anything just pass the control value in query such as @gender and so on.





    Friday, September 17, 2010 6:20 AM
  • User-1199946673 posted

    string sql = "UPDATE Member" + 

                             "SET fullname=?, gender=?, DOB=?, address=?, hp=?, email=?, question=?, answer=?" + 

                             "WHERE username=?";

     

    There's no space before SET and WHERE

    string sql = "UPDATE Member" + 

                             " SET fullname=?, gender=?, DOB=?, address=?, hp=?, email=?, question=?, answer=?" + 

                             " WHERE username=?";

    why are u using ?
     

     

    Why Not? The questionmark is the default Parameter in Access. Every questionmark is threaded as a different parameter

     

    where is parameter 

      string sql = "UPDATE Member" + 

                             "SET fullname=@FullName, gender=@Gender, DOB=@DOB, address=@address, hp=@hp, email=@email, question=?, answer=@answer" + 

                             "WHERE username=@Username";

     

    This is exactly the same as using questionmarks

    cmd.Parameters.AddWithValue("@Gender", rblGender.SelectedValue);

    Because OleDb Parameters are recognized by position only, you don't need to specify the name! It can be blank, or any name you like, it really doesn't change anything!

    Hi,

    Please try this...

     

    Please don't!!!!!

    Read this:

    http://www.mikesdotnetting.com/Article/26/Parameter-Queries-in-ASP.NET-with-MS-Access

    Friday, September 17, 2010 8:10 AM
  • User-721945135 posted

    Thanks guy. 

    Thanks for reply me, I have try the solutions but none of it is work.

    Saturday, September 18, 2010 10:44 PM
  • User-1199946673 posted

    I have try the solutions but none of it is work.
     

    What is your latest code, and what error do you get?

    Sunday, September 19, 2010 5:24 AM
  • User-721945135 posted

    hans v, there are no error message show and my latest codes are same as i posted.

    Sunday, September 19, 2010 10:52 PM
  • User-721945135 posted

    hans v, 
    I know what cause my update button not function, it is a codes that used to display user data during page load under "protected void Page_Load(object sender, EventArgs e)" then i add a code like "if (!Page.IsPostBack)" and i put all those codes inside it.
    Now my update button is work.

    hans v, 

    I know what cause my update button not function, it is a codes that used to display user data during page load under "protected void Page_Load(object sender, EventArgs e)" then i add a code like "if (!Page.IsPostBack)" and i put all those codes inside it.

    Now my update button is work.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 20, 2010 1:39 AM