1

I've been trying to look around and fix this and I've tried multiple things for hours, so I decided I'll ask others. I'm getting a

'Syntax error in UPDATE statement.'

When clicking the save button.

Here is my code:

OleDbCommand command = new OleDbCommand();
command.Connection = connection;

string query = "update Profiles set [PROFILE NAME]='" + textBox1.Text + "', [LOGIN EMAIL]='" + textBox2.Text + "', [PASSWORD]='" + textBox3.Text + "', [FULL NAME]='" + textBox4.Text + "', [CARD NUMBER]='" + textBox5.Text + "', [EXP MONTH]='" + comboBox1.Text + "', [EXP YEAR]='" + comboBox2.Text + "', CVV='" + textBox6.Text + "', where ID=" + textBox7.Text + "";
command.CommandText = query;
command.ExecuteNonQuery();
connection.Close();
MessageBox.Show("Profile Saved");
RefreshDBConnection();

Updated Code:

ConnectToDataBase();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;

//string query = "update Profiles set [PROFILE NAME]='" + textBox1.Text + "', [LOGIN EMAIL]='" + textBox2.Text + "', [PASSWORD]='" + textBox3.Text + "', [FULL NAME]='" + textBox4.Text + "', [CARD NUMBER]='" + textBox5.Text + "', [EXP MONTH]='" + comboBox1.Text + "', [EXP YEAR]='" + comboBox2.Text + "', CVV='" + textBox6.Text + "' where  ID='" + Convert.ToInt32(textBox7.Text) + "'";
string query = "update Profiles set [PROFILE NAME]= @Profile, [LOGIN EMAIL]= @Email, [PASSWORD]= @Pass, [FULL NAME]= @Name, [CARD NUMBER]= @Card, [EXP MONTH]= @EXPM, [EXP YEAR]= @EXPY, CVV= @CVV where ID = '" +textBox7.Text+ "'";
command.Parameters.AddWithValue("@Profile", textBox1.Text);
command.Parameters.AddWithValue("@Email", textBox2.Text);
command.Parameters.AddWithValue("@Pass", textBox3.Text);
command.Parameters.AddWithValue("@Name", textBox4.Text);
command.Parameters.AddWithValue("@Card", Convert.ToInt32(textBox5.Text));
command.Parameters.AddWithValue("@EXPM", Convert.ToInt32(comboBox1.Text));
command.Parameters.AddWithValue("@EXPY", Convert.ToInt32(comboBox2.Text));
command.Parameters.AddWithValue("@CVV", Convert.ToInt32(textBox6.Text));
command.CommandText = query;
command.ExecuteNonQuery();
connection.Close();
MessageBox.Show("Profile Saved");
RefreshDBConnection();
this.Close();
6
  • Are you aware of what a SQL Injection attack is? Commented Nov 10, 2018 at 7:35
  • I have no idea, I changed though. Commented Nov 10, 2018 at 9:08
  • It's where someone can type valid SQL in the textBox1 TextBox, for example, and your code would allow their custom SQL to be run on the server. It could query sensitive information or drop the database or insert new records in a user admin table. Commented Nov 10, 2018 at 9:18
  • Oh wow, well this is all going to be saved on the machine local to the user, no server or anything Commented Nov 10, 2018 at 9:22
  • 1
    It's good practice to use parameters to avoid this kind of thing. The number of times that code that was only ever meant to be local that ends up going to production astounds me. Commented Nov 10, 2018 at 9:24

3 Answers 3

1

You have one extra comma , before your Where statement:

CVV='" + textBox6.Text + "', where 

Just remove it. And you should convert your textBox7.Text to int, if it's type is integer, ID= '" + Convert.ToInt32(textBox7.Text) + "' (don't forget to surround it with single quotes). Also you should always use parameterized queries to avoid SQL Injection. Something like this:

string query = "update Profiles set [PROFILE NAME]= @Profile,... where ID = @Id";
command.Parameters.AddWithValue("@Profile", textBox1.Text);
command.Parameters.AddWithValue("@Id", textBox7.Text);//Or Convert.ToInt32(textBox7.Text)

Although specify the type directly and use the Value property is more better than AddWithValue:

command.Parameters.Add("@Profile", SqlDbType.VarChar).Value = textBox1.Text;
command.Parameters.Add("@Id", SqlDbType.Int).Value = Convert.ToInt32(textBox7.Text);

And of course, it has been recommended to use using statement always.

Sign up to request clarification or add additional context in comments.

6 Comments

now I get 'Data type mismatch in criteria expression.'
@CadenBuckelew Did you try this? where ID= '" + Convert.ToInt32(textBox7.Text) + "'
@CadenBuckelew Also using Add instead of AddWithValue can help your more. Again check my updated answer.
@CadenBuckelew And you should use parameters with your ID as well. Check my updated answer to see how.
After a lot of trial and error it worked, thank you so much! sorry for being new at coding, this is my first week or so learning how to use c#. I really appreciate the help!
|
1

Another reason could be, that the values you read from the textboxes could contain special characters which will make the syntax invalid when beiing concatenated to the SQL string. This you also can avoid when using parameter queries.

3 Comments

I added the parameter queries and I get 'Data type mismatch in criteria expression.'
Please show us the whole resulting query you have so far. And a valuable/necessary additional information would be the datatype of the fields of the table.
Because you get an'Data type mismatch in criteria expression we still would need the data types of the fields of the table. And your criteria still uses string concatenation.
0
CVV='" + textBox6.Text + "', where 

you have to delete comma here. also its better to use parameters since you have several. sending them like that will cause problems in future. so i suggest you to use cmd.Parameters.Add(); instead of raw usage. also if ID is Integer you have to Convert.ToInt32(textBox7.Text);

2 Comments

I now get this error: 'Data type mismatch in criteria expression.'
It's because you sending all parameters as string. which db sees as nvarchar so its occurs. you gotta convert it all to related field's data type. like if integer Convert.ToInt32(textbox7.text);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.