I am trying to save all my data in a DataGridView into an Access database but I'm getting this error: Syntax error in INSERT INTO statement.
My code follows:
try
{
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\MotoFix.mdb;";
OleDbCommand Com = new OleDbCommand();
for (int i = 0; i < gridViewProduct_Transac.Rows.Count - 1; i++)
{
using (OleDbConnection Con = new OleDbConnection(ConnectionString))
{
string str;
Con.Open();
str = @"INSERT INTO Transaction(tranNo, custID, pID, userID, orderNo, sDate) VALUES ('" + gridViewProduct_Transac.Rows[i].Cells["Transaction Number"].Value + "', '" + gridViewProduct_Transac.Rows[i].Cells["Customer ID"].Value + "'," + gridViewProduct_Transac.Rows[i].Cells["Referral ID"].Value + ",'" + gridViewProduct_Transac.Rows[i].Cells["Cashier ID"].Value + "','" + gridViewProduct_Transac.Rows[i].Cells["Order Number"].Value + "','" + gridViewProduct_Transac.Rows[i].Cells["Date"].Value + "');";
using (Com = new OleDbCommand(str, Con))
{
Com.ExecuteNonQuery();
Con.Close();
}
}
}
}
If sDate is a date type you may need to format it - Format("YYYY-mm-DD",sDate) or Format("YYYY-mm-DD HH:MM:ss",sDate)