Search code examples
c#oledboledbconnectionoledbdataadapteroledbexception

how to set DateTime.now as current time in Access DataBase - c# windows forms app


Im having problems with An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll ---> data type mismatch in criteria expression when i try to set the date and time of user's arrival by pressing on button.

How can i set the current date and time of arrival, without getting this error?

A part of the code

bool clockedIn = false;
public void button1_Click(object sender, EventArgs e)
    {
        if (!clockedIn)
        {

            OleDbConnection myConnection = new OleDbConnection("\\CONNECTION PATH");
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = myConnection;
            cmd.CommandText = "Insert into Weekdays (Arrival)" + "values(@Arrival)";


            cmd.Parameters.AddWithValue("@Arrival", DateTime.now);

            myConnection.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("Arrival added.");
            myConnection.Close();
        }

}


Solution

  • string query = "insert into table(data) values('"+DateTime.Now.ToString("yyyy-MM-dd")+"')";
    
    OleDbConnection conexao = new OleDbConnection();
    conexao.ConnectionString = this.string_conexao;
    conexao.Open();
    
    OleDbCommand comando = new OleDbCommand();
    comando.CommandText = query;
    comando.Connection = conexao;
    try
    {
    comando.ExecuteNonQuery();
    }
    catch
    {
    }