Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
1.70/5 (3 votes)
See more:
C#
OleDbConnection con = new OleDbConnection(@" provider=Microsoft.ace.Oledb.12.0; data source=\\sisc-erelim\4_Printing\VTDB\DB\VirginiTEADB2.accdb; Persist Security Info=False");

private void button1_Click(object sender, EventArgs e)
{
   try
   {
      OleDbCommand cmd = new OleDbCommand();
      cmd.CommandType = CommandType.Text;
      cmd.CommandText = "INSERT INTO Accountstbl (Username, Password)" + "VALUES ('" + textBox1.Text + "','" + textBox2.Text + "')";
      cmd.Parameters.AddWithValue("@Username", textBox1.Text);
      cmd.Parameters.AddWithValue("@Password", textBox2.Text);
      cmd.Connection = con;
      con.Open();
      cmd.ExecuteNonQuery();
      }
     catch (Exception ex)
     {
         textBox1.Text = ex.ToString();
     }

And I always got this error,

System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement.
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
at VirginiTEAcorp.Form3.button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\12-014s\My Documents\applications\Database\WindowsFormsApplication1\Form3.cs:line 34
thanks :)
Posted
Updated 11-Feb-13 22:25pm
v3
Comments
Kiran Susarla 12-Feb-13 4:13am    
Where did you define the parameters in the command text?

Update your insert statement like this
C#
cmd.CommandText = "INSERT INTO Accountstbl (Username, Password)" + " VALUES (@Username,@Password)";

this is because you are already passing the arguments to the query parameter
C#
cmd.Parameters.AddWithValue("@Username", textBox1.Text);
cmd.Parameters.AddWithValue("@Password", textBox2.Text);
 
Share this answer
 
Comments
Joezer BH 12-Feb-13 4:57am    
5+
Jibesh 12-Feb-13 4:59am    
Thank you Edo Tzumer
alfred sanz 12-Feb-13 20:12pm    
This is the answer, but there is an error accepting the answer! :( thanks anyway this solve my problem
Jibesh 12-Feb-13 20:13pm    
Glad that helped.
You have added parameters into your command, but you have injected the values into the SQL. Change your command text into
SQL
INSERT INTO Accountstble (Username, Password) VALUES (?, ?)
Then, remove the @ symbols from the parameter names - they aren't needed as they are used for SQL Server, not Access.
 
Share this answer
 
Comments
Joezer BH 12-Feb-13 4:56am    
5+
Pete O'Hanlon 12-Feb-13 5:04am    
Thanks Edo.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900