C# .NET - Error in Update Query

Asked By Tausif on 15-Apr-11 07:48 AM
Hi,

I am creating a simple login form where i am updatind the records of the login people.
My query is

OleDbCommand updatecmd = new OleDbCommand("Update Login SET Password ='" + txtPassword.Text + "' where Username='" + txtUsername.Text + "'", con);

I dont know where i am goin wrong. I am getting error as Syntax Error in Update Statement.
Can you please help me with this.
Nikhil Mahajan replied to Tausif on 15-Apr-11 07:50 AM
hey tausif,
you update command is fine..
what is the exact error you are getting.......let me know
dipa ahuja replied to Tausif on 15-Apr-11 08:55 AM
error is here:

OleDbCommand updatecmd = new OleDbCommand("Update Login SET Password ='" + txtPassword.Text + "' where Username='" + txtUsername.Text + "'", con);

Put the single ' before the "

so correct query is:

OleDbCommand updatecmd = new OleDbCommand("Update Login SET Password ='" + txtPassword.Text + '" where Username='" + txtUsername.Text + "'", con);
dipa ahuja replied to Tausif on 15-Apr-11 08:57 AM

Code was cut:

error is here:

 

OleDbCommand updatecmd = new OleDbCommand("Update Login SET Password ='" 

+ txtPassword.Text + "' where Username='" + txtUsername.Text + "'", con);

 

Put the single ' before the "

 

so correct query is:

 

OleDbCommand updatecmd = new OleDbCommand("Update Login SET Password ='" 

+ txtPassword.Text + '" where Username='" + txtUsername.Text + "'", con);

Tausif replied to dipa ahuja on 15-Apr-11 11:18 AM
Hi Dipa,

Can you please check the correct query which you have given me?
It is showing an error if you check.
And please run the query once on your side and let me know thanx.
Tausif replied to Nikhil Mahajan on 15-Apr-11 11:21 AM
Hi Nikhil,

Thanks for your reply i also know that the query is correct but still i am getting Error as "SYNTAX ERROR IN UPDATE STATEMENT". I am also looking and making changes in the query and as per my view the query is correct but i dont know where i am goin wrong. Will be thankful if you run and check. Waiting for your reply.
Nikhil Mahajan replied to Tausif on 15-Apr-11 12:47 PM
hey tausif,,

as i  told you there is nothing wrong in query....its just simple update query....

there might be some other problem with your code... so why dont you show us the full code .. so that we can help you to rectify the problem....
Tausif replied to Nikhil Mahajan on 15-Apr-11 11:24 PM

Hi Nikhil,
The below is the code and i have mentioned the line where i am getting error.
My database table name is Login as in the query.
Please check this and let me know.....
////code to update
private void button2_Click(object sender, EventArgs e)

{

try

{

con.Open();

OleDbCommand updatecmd = new OleDbCommand("Update Login SET Password ='" + txtPassword.Text + "' where Username='" + txtUsername.Text + "'", con);

int i = updatecmd.ExecuteNonQuery();  // i m getting error on this line.

if (i == 1)

{

MessageBox.Show("Record Updated");

con.Close();

}

}

catch (Exception exp)

{

MessageBox.Show(exp.ToString());

}

finally

{

con.Close();

}

dipa ahuja replied to Tausif on 16-Apr-11 10:06 AM
What the error you getting ? Post that error here..
Tausif replied to dipa ahuja on 18-Apr-11 06:08 AM
Hi Dipa,

The error i am getting is 'ERROR IN UPDATE STATEMENT'. You can check my previous mails.
And if you want you can call me on my no. Reply me or you give me your no. Thanks.