1
Answer

want to launch a query that will write from C# To Access

Sarah Good

Sarah Good

8y
259
1

 I want to launch a query that will write a 3 fields on a C# winforms back to an ms-access database


 Pressing the button Add Time Sheet executes the query and returns an error Syntax error comma in query expression

 
ERRORSystem.Data.OleDb.OleDbException (0x80040E14): Syntax error (comma) in query expression '('6/4/2016', '99', 'Rami')'. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) ...
 
 

You can see the error in the linked screenshot and a part of it above.

When I try put one field in the query my query writes back to access database successfully. it only gives me the comma error when i try to insert multiple fields

 
 
 
 
Below is a Copy Past of the code in the image above.
 
 
 private void button1_Click(object sender, EventArgs e)
 
{
    try
 
     {
connection.Close();
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection; command.CommandText = "INSERT INTO ResourceWorkHours (WorkWeek, HoursWorked,FirstName) SELECT ('" + ddlWeeks2.Text + "','" + txtWorkHours.Text + "','" + cbResourceFirstName.Text + "') FROM Resources INNER JOIN ResourceWorkHours ON Resources.ResourceID = ResourceWorkHours.ResourceID";
 
command.ExecuteNonQuery();
MessageBox.Show("Employee Added to Database");
connection.Close();

}catch
(Exception ex){ MessageBox.Show("ERROR" + ex); } connection.Close();
      }
}
 
Answers (1)