2

I am Trying to Update multiple rows in a MS Access DB But every time it gives ERROR IN UPDATE SYNTAX here's my code

cn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source =C:\\Users\\Kalpesh\\Desktop\\Entry.accdb");
sqlstr = "Update Members SET Name=@Name, MobileNo=@MobileNo, Password=@Password, IDType=@IDType, IDNo=@IDNo, AmountPaid=@AmountPaid, Address=@Address, Reg Date = "+DateTime.Now.ToShortDateString()+" WHERE UserName = '" + comboBox4.SelectedItem.ToString() + "'",cn);
cmd = new OleDbCommand(sqlstr, cn);
cmd.Parameters.AddWithValue("@Name", textBox19.Text.ToString());
cmd.Parameters.AddWithValue("@MobileNo", textBox14.Text.ToString());
cmd.Parameters.AddWithValue("@Password", textBox17.Text.ToString());
cmd.Parameters.AddWithValue("@IDType", comboBox5.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@IDNo", textBox11.Text.ToString());
cmd.Parameters.AddWithValue("@AmountPaid", textBox16.Text.ToString());
cmd.Parameters.AddWithValue("@Address", richTextBox3.Text.ToString());

cn.Open();
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
MessageBox.Show("Record Updated");
cn.Close();
2
  • I am not an expert in Access but Reg Date syntax looks wrong. First, there is a space in identifier. Second, you are probably missing some punctuation around the date literal. Commented Apr 17, 2014 at 12:09
  • Update Working Code sqlstr = "Update Members SET [Name]=@Name, MobileNo=@MobileNo, [Password]=@Password, IDType=@IDType, IDNo=@IDNo, AmountPaid=@AmountPaid, Address=@Address, [Reg Date] = '"+DateTime.Now.ToShortDateString()+"' WHERE UserName = '" + comboBox4.SelectedItem.ToString() + "'"; Commented Apr 17, 2014 at 12:10

3 Answers 3

4

Name and Password are reserved keywords in MS Access.

If a reserved word is already in use, you can avoid error messages by surrounding each occurrence of the word with brackets ([ ]). However, the best solution is to change the name to a nonreserved word.

You need to use Reg Date with square brackets like [Reg Date] also since it has space. Parameterized your Reg Date and UserName column values as you did for other columns. Like;

... [Reg Date] = @reg WHERE UserName = @user ...
cmd.Parameters.AddWithValue("@reg", DateTime.Now.ToShortDateString());
cmd.Parameters.AddWithValue("@user", comboBox4.SelectedItem.ToString());

Also use using statement to dispose your OleDbConnection and OleDbCommand.

string conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =C:\\Users\\Kalpesh\\Desktop\\Entry.accdb";
using(OleDbConnection cn = new OleDbConnection(conString))
using(OleDbCommand cmd = cn.CreateCommand())
{
  // Do your work..
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hell Yes You were right it adding [] fro name & password & single quot for datetime worked
0

Since there is no clear error message about the syntax problem, I would guess its the lack of single quotes around the date, try this:

RegDate = '"+DateTime.Now.ToShortDateString()+"'

Comments

0

Problem is here

 "Reg Date = "+DateTime.Now.ToShortDateString()+"

Try this

 "[Reg Date] = #" + DateTime.Now.ToShortDateString() + "# "

Comments

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.