locked
Error in C#: Data type mismatch in criteria expression when insert Record into Access Table ? RRS feed

  • Question

  • Suppose I have an employee table (TABEMPLOYEE) with the following fields:

    IDEMPLOYEE  type Number = Long Integer
    USERNAME     type Text = 50
    DATEWORK    type  Date/time = General Date
    NOTES             type Memo
    FONTNAME    type  Text = 50
    FONTSIZE       type  Text = 50

    case 1: when saving into TABEMPLOYEE the field IDEMPLOYEE = 9, why results IDEMPLOYEE = 9 while I idEMPLOYEE = 1

    sSQL = "INSERT INTO TABEMPLOYEE (IDEMPLOYEE) VALUES (@IDEMPLOYEE);";
    OleDbCommand cmd = new OleDbCommand(sSQL, sConnecACCESS);
    try
       {
            idEMPLOYEE = 1;
            cmd.Parameters.AddWithValue("@IDEMPLOYEE", idEMPLOYEE);
            //Or
            //cmd.Parameters.Add("@IDEMPLOYEE", OleDbType.Integer).Value = idEMPLOYEE; 
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
       }
    catch (OleDbException ex)
       {
             MessageBox.Show("Error: " + ex.Message);
       }

    case 2: Results: Data type mismatch in criteria expression

    sSQL = "INSERT INTO TABEMPLOYEE (IDEMPLOYEE, USERNAME) VALUES (@IDEMPLOYEE, @USERNAME);";
    OleDbCommand cmd = new OleDbCommand(sSQL, sConnecACCESS);
    try
       {
            idEMPLOYEE = 1;
            cmd.Parameters.AddWithValue("@IDEMPLOYEE", idEMPLOYEE);
            //Or
            //cmd.Parameters.Add("@IDEMPLOYEE", OleDbType.Integer).Value = idEMPLOYEE;
            cmd.Parameters.AddWithValue("@@MUSERNAME", dt.Rows[0]["USERNAME"].ToString());
            //Or
            //cmd.Parameters.Add("@MUSERNAME", OleDbType.VarChar).Value = dt.Rows[0]["USERNAME"].ToString();
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
       }
    catch (OleDbException ex)
       {
             MessageBox.Show("Error: " + ex.Message);
       }

    case 3: same way i replace USERNAME tape DATEWORK. Results: Data type mismatch in criteria expression

    try
       {
            idEMPLOYEE = 1;
            cmd.Parameters.AddWithValue("@IDEMPLOYEE", idEMPLOYEE);
    
            cmd.Parameters.AddWithValue("@DATEWORK", DateTime.Now);
            //Or
            //cmd.Parameters.Add("@DATEWORK", 
            OleDbType.DBDate).Value = DateTime.Now;
     }
    catch (OleDbException ex)
       {
             MessageBox.Show("Error: " + ex.Message);
       }
    
    Other cases I replaced similar to the above give an error general results, you know how to make this error and how to fix it ?


    Thursday, June 6, 2019 4:49 AM