You must Sign In to post a response.
  • Category: .NET

    Syntax error in update statement

    Hi all,

    I am using windows application with ms-access DB
    When i am trying to update the query it shows error as syntax error in update statement

    Code is:------------

    cmd.CommandText = "UPDATE Wsr_UserDetails SET Password='" & txtnpwd.Text & "',Confirm_Password='" & txtcpwd.Text & "',First_Login =1 WHERE UserId =" & UserId

    cmd.CommandType = CommandType.Text
    cmd.Connection = con
    cmd.ExecuteNonQuery()-------------> Getting error here

    Thanks in Advance
    Sucharitha
  • #680972
    Try this code

    cmd.CommandText = "UPDATE Wsr_UserDetails SET Password='" + txtnpwd.Text + "',Confirm_Password='" + txtcpwd.Text + "',First_Login =1 WHERE UserId =" + UserId


    Hope this will help you

  • #680977
    Here is the sample code for the same.


    private void button1_Click(object sender, EventArgs e)
    {
    string pwd = txtnpwd.Text;
    string cpwd = txtcpwd.Text;
    string str = "UPDATE Wsr_UserDetails SET Password='" + + "',Confirm_Password='" + + "',First_Login = 1 WHERE UserId =" + UserId;
    OleDbCommand c = new OleDbCommand(str , conn);
    c.ExecuteNonQuery();
    conn.Close();
    }



    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #680980
    Hi All,

    Thanks your response
    But i am using vb.net not C#

  • #681006
    Make sure that the character ' is not in the password string. It could lead to syntax error. Looking the query I can't see any syntax error except the fact that apostrophe character is typed by user in the text box.
    Regards
    UM

  • #681078
    Rewrite your query like below


    Dim qry as String
    qry= "UPDATE Wsr_UserDetails SET Password='" & txtnpwd.Text & "', Confirm_Password='" & txtcpwd.Text & "', First_Login =1 WHERE UserId = '" & UserId & "'";
    sqlcon.Open()
    sqlcmd = New SqlCommand(qry, sqlcon)
    sqlcmd.CommandType = CommandType.Text
    sqlcmd.ExecuteNonQuery()
    sqlcon.Close()

    Regards
    N.Ravindran
    Your Hard work never fails

  • #681179
    Hi Everyone,

    Password is a reserved work,so i have changed the colums name as PWD

    Now its working fine,but not updated in Database.

    **************
    con.Open()
    cmd = New OleDbCommand("UPDATE Wsr_UserDetails SET PWD='" & txtnpwd.Text & "',First_Login =1 WHERE UserId=" & lbluid & ";", con)
    cmd.ExecuteNonQuery()
    con.Close()

  • #681270
    Make sure that UserId is available in the database
    Regards
    N.Ravindran
    Your Hard work never fails


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.