I'm trying to modify records in my Microsoft Access database through c# in the .Net Framework. This is my execution code, but I keep getting a Syntax Error from my INSERT statement and can't figure out why, can anyone help? I'm also experiencing some trouble where the SELECT statement, isn't returning any data which I KNOW exists in the database. I've been at this for days and need a fresh pair of eyes, what could I be doing wrong?
This is the error:
Error: System.Data.OleDb.OleDbException (0x80040E14): 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 ABM_Office_System.UpdateABMDatabaseWindow.b__0()...
for (int i = 0; i < hmuFinalList.Count; i++)
{
Dispatcher.Invoke(() =>
{
stsW.StatusLbl.Content = "Updating " + hmuFinalList[i].MName + "'s records";
});
readcmd.Parameters.Clear();
readcmd.CommandText = "SELECT * FROM [ABM] WHERE [M Name]=@";
readcmd.Parameters.AddWithValue("@", hmuFinalList[i].MName);
Console.WriteLine("READCMD: " + readcmd.CommandText);
Console.WriteLine("ReadcmdTRANSACTION: ");
affected = readcmd.ExecuteNonQuery();
Console.WriteLine("affected: " + affected.ToString());
if (affected > 0) //UPDATE TIME!
{
executecmd.Parameters.Clear();
executecmd.CommandText = "UPDATE [ABM] SET "+
"[M Type]=@," +
"[M Component]=@," +
"[M Birth Date]=@," +
"[M Visa Expiration Date]=@," +
"[M Residence Nation]=@," +
"[M MTC]=@," +
"[M MTC Date]=@," +
"[M Arrival Date]=@," +
"[M Release Date]=@, " +
"[M Past Position]=@," +
"[M Past Position Start Date]=@," +
"[M Current Position Start Date]=@," +
"[M Past Area]=@," +
"[M Past Area Start Date],=@" +
"[M Current Area Start Date]=@," +
"[M Title - English]=@," +
"[M Title - Non-English]=@," +
"[M HMU Correspondent Type]=@," +
"[M HMU Correspondent Last Name]=@," +
"[M HMU Correspondent Given Name]=@," +
"[M HMU Correspondent Language]=@," +
"[M HMU Correspondent Comment]=@," +
"[M HMU Correspondent Title]=@," +
"[M HMU Correspondent Relationship]=@," +
"[M HMU Correspondent Pronoun - Subject]=@," +
"[M HMU Correspondent Pronoun - Object]=@," +
"[M HMU Correspondent Pronoun - Possessive]=@," +
"[M HMU Correspondent Pronoun - Other]=@," +
"[M Previous Address Line 1]=@," +
"[M Previous Address City]=@," +
"[M Previous Address State/Province]=@,"
+ "[M Previous Address Postal Code]=@," +
"[M Previous Address Country]=@," +
"[M Type Plural]=@," +
"[M Transfer Effective Date]=@," +
"[M Position is a Promotion]=@," +
"[M HMU Correspondent Pronoun Cap - Subject]=@," +
"[M HMU Correspondent Pronoun Cap - Object]=@," +
"[M HMU Correspondent Pronoun Cap - Possessive]=@," +
"[M HMU Correspondent Pronoun Cap - Other]=@," +
"[M HMU Correspondent Title - Envelope]=@," +
"[M Where Born]=@" +
"WHERE [M Name]=@";
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MType) ? (object)DBNull.Value : hmuFinalList[i].MType);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MComponent) ? (object)DBNull.Value : hmuFinalList[i].MComponent);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MBirthDate) ? (object)DBNull.Value : hmuFinalList[i].MBirthDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MVisaExpirationDate) ? (object)DBNull.Value : hmuFinalList[i].MVisaExpirationDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MResidenceNation) ? (object)DBNull.Value : hmuFinalList[i].MResidenceNation);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MMTC) ? (object)DBNull.Value : hmuFinalList[i].MMTC);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MMTCDate) ? (object)DBNull.Value : hmuFinalList[i].MMTCDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MArrivalDate) ? (object)DBNull.Value : hmuFinalList[i].MArrivalDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MReleaseDate) ? (object)DBNull.Value : hmuFinalList[i].MReleaseDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPastPosition) ? (object)DBNull.Value : hmuFinalList[i].MPastPosition);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPastPositionStartDate) ? (object)DBNull.Value : hmuFinalList[i].MPastPositionStartDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MCurrentPositionStartDate) ? (object)DBNull.Value : hmuFinalList[i].MCurrentPositionStartDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPastArea) ? (object)DBNull.Value : hmuFinalList[i].MPastArea);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPastAreaStartDate) ? (object)DBNull.Value : hmuFinalList[i].MPastAreaStartDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MCurrentAreaStartDate) ? (object)DBNull.Value : hmuFinalList[i].MCurrentAreaStartDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MTitleEnglish) ? (object)DBNull.Value : hmuFinalList[i].MTitleEnglish);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MTitleNonEnglish) ? (object)DBNull.Value : hmuFinalList[i].MTitleNonEnglish);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentType) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentType);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentLastName) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentLastName);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentGivenName) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentGivenName);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentLanguage) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentLanguage);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentComment) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentComment);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentTitle) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentTitle);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentRelationship) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentRelationship);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounSubject) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounSubject);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounObject) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounObject);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounPossessive) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounPossessive);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounOther) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounOther);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressLine1) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressLine1);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressCity) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressCity);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressStateProvince) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressStateProvince);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressPostalCode) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressPostalCode);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressCountry) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressCountry);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MTypePlural) ? (object)DBNull.Value : hmuFinalList[i].MTypePlural);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MTransferEffectiveDate) ? (object)DBNull.Value : hmuFinalList[i].MTransferEffectiveDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPositionisaPromotion) ? (object)DBNull.Value : hmuFinalList[i].MPositionisaPromotion);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounCapSubject) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounCapSubject);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounCapObject) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounCapObject);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounCapPossessive) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounCapPossessive);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounCapOther) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounCapOther);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentTitleEnvelope) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentTitleEnvelope);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MWhereBorn) ? (object)DBNull.Value : hmuFinalList[i].MWhereBorn);
executecmd.Parameters.AddWithValue("@", hmuFinalList[i].MName);
Console.WriteLine("HMU UPDATE EXECUTION: " + executecmd.CommandText);
executecmd.ExecuteNonQuery();
}
else //INSERT TIME!
{
executecmd.Parameters.Clear();
executecmd.CommandText = "INSERT INTO [ABM] ([M Name],[M Type],[M Component],[M Birth Date],[M Visa Expiration Date],[M Residence Nation],"+
"[M MTC]," +
"[M MTC Date],"+
"[M Arrival Date],"+
"[M Release Date], "+
"[M Past Position],"+
"[M Past Position Start Date]," +
"[M Current Position Start Date]," +
"[M Past Area],"+
"[M Past Area Start Date],"+
"[M Current Area Start Date],"+
"[M Title - English],"+
"[M Title - Non-English],"+
"[M HMU Correspondent Type],"+
"[M HMU Correspondent Last Name]," +
"[M HMU Correspondent Given Name]," +
"[M HMU Correspondent Language]," +
"[M HMU Correspondent Comment]," +
"[M HMU Correspondent Title]," +
"[M HMU Correspondent Address Line 1]," +
"[M HMU Correspondent Relationship]," +
"[M HMU Correspondent Pronoun - Subject]," +
"[M HMU Correspondent Pronoun - Object]," +
"[M HMU Correspondent Pronoun - Possessive]," +
"[M HMU Correspondent Pronoun - Other]," +
"[M Previous Address Line 1],"+
"[M Previous Address City]," +
"[M Previous Address State/Province],"+
"[M Previous Address Postal Code]," +
"[M Previous Address Country],"+
"[M Type Plural]," +
"[M Transfer Effective Date],"+
"[M Position is a Promotion]," +
"[M HMU Correspondent Pronoun Cap - Subject]," +
"[M HMU Correspondent Pronoun Cap - Object]," +
"[M HMU Correspondent Pronoun Cap - Possessive]," +
"[M HMU Correspondent Pronoun Cap - Other]," +
"[M HMU Correspondent Title - Envelope]," +
"[M Where Born],)"+
"VALUES(@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@)";
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MName) ? (object)DBNull.Value : hmuFinalList[i].MName);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MType) ? (object)DBNull.Value : hmuFinalList[i].MType);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MComponent) ? (object)DBNull.Value : hmuFinalList[i].MComponent);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MBirthDate) ? (object)DBNull.Value : hmuFinalList[i].MBirthDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MVisaExpirationDate) ? (object)DBNull.Value : hmuFinalList[i].MVisaExpirationDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MResidenceNation) ? (object)DBNull.Value : hmuFinalList[i].MResidenceNation);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MMTC) ? (object)DBNull.Value : hmuFinalList[i].MMTC);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MMTCDate) ? (object)DBNull.Value : hmuFinalList[i].MMTCDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MArrivalDate) ? (object)DBNull.Value : hmuFinalList[i].MArrivalDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MReleaseDate) ? (object)DBNull.Value : hmuFinalList[i].MReleaseDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPastPosition) ? (object)DBNull.Value : hmuFinalList[i].MPastPosition);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPastPositionStartDate) ? (object)DBNull.Value : hmuFinalList[i].MPastPositionStartDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MCurrentPositionStartDate) ? (object)DBNull.Value : hmuFinalList[i].MCurrentPositionStartDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPastArea) ? (object)DBNull.Value : hmuFinalList[i].MPastArea);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPastAreaStartDate) ? (object)DBNull.Value : hmuFinalList[i].MPastAreaStartDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MCurrentAreaStartDate) ? (object)DBNull.Value : hmuFinalList[i].MCurrentAreaStartDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MTitleEnglish) ? (object)DBNull.Value : hmuFinalList[i].MTitleEnglish);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MTitleNonEnglish) ? (object)DBNull.Value : hmuFinalList[i].MTitleNonEnglish);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentType) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentType);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentLastName) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentLastName);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentGivenName) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentGivenName);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentLanguage) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentLanguage);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentComment) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentComment);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentTitle) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentTitle);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentAddressLine1) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentAddressLine1);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentRelationship) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentRelationship);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounSubject) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounSubject);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounObject) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounObject);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounPossessive) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounPossessive);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounOther) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounOther);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressLine1) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressLine1);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressCity) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressCity);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressStateProvince) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressStateProvince);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressPostalCode) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressPostalCode);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressCountry) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressCountry);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MTypePlural) ? (object)DBNull.Value : hmuFinalList[i].MTypePlural);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MTransferEffectiveDate) ? (object)DBNull.Value : hmuFinalList[i].MTransferEffectiveDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPositionisaPromotion) ? (object)DBNull.Value : hmuFinalList[i].MPositionisaPromotion);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounCapSubject) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounCapSubject);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounCapObject) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounCapObject);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounCapPossessive) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounCapPossessive);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounCapOther) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounCapOther);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentTitleEnvelope) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentTitleEnvelope);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MWhereBorn) ? (object)DBNull.Value : hmuFinalList[i].MWhereBorn);
Console.WriteLine("HMU INSERT EXECUTION: " + executecmd.CommandText);
executecmd.ExecuteNonQuery();
}
}
This is driving me crazy!
Your insert column list ends with a comma when it shouldn't.
Replace
"[M Where Born],)"+
With
"[M Where Born])"+
User contributions licensed under CC BY-SA 3.0