I try to save the username and DateTime object in the MS-Access database, but I get a "Syntax error in the INSERT INTO statement". When I debug a program, I see that the date is correctly transmitted. In addition, I completely deleted the DateTime information and used the insert command only with the username and saved it correctly, so it has something to do with DateTime itself, but I don't know what. Here is the related code:
private void btnBeginProgram_Click(object sender, EventArgs e)
{
busObject.DtDate = DateTime.Now;
busObject.UserName = txtEnterName.Text;
busObject.SaveData();
this.Close();
}
public string UserName
{
get { return userName; }
set { userName = value; }
}
public DateTime DtDate
{
get { return dtDate; }
set { dtDate = value; }
}
public void SaveData()
{
ProgramLoginDAL.SaveData(this);
}
public static void SaveData(ProgramLoginBOL busObject)
{
try
{
OleDbCommand cmd = aConnection.CreateCommand();
String sSQLCommand = "INSERT INTO ProgramLogin (UserName, DateTime) VALUES (?,?)";
cmd.Parameters.AddWithValue("?", busObject.UserName);
cmd.Parameters.AddWithValue("?", busObject.DtDate);
if (aConnection.State == ConnectionState.Closed)
{
aConnection.Open();
}
cmd.CommandText = sSQLCommand;
cmd.ExecuteNonQuery();
aConnection.Close();
MessageBox.Show("Data Saved");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
MessageBox.Show("Error! Data was not saved.");
}
}
source
share