简体   繁体   中英

data-type-mismatch-in-criteria-expression-error when delete from MS Access with ADO.NET C#

i write code that insert and delete some data with Microsoft Access database , i can insert the data but when i delete it i have an error "data-type-mismatch-in-criteria-expression" i don't know why !!! Any one help me ?

thanks in advance ;

  private void Savebt_Click(object sender, EventArgs e)
{
    //try
    //{
    OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\me\Library Store\Library Store\Store.accdb");
     try
{
    conn.Open();

    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = conn;
    cmd.CommandText = "INSERT INTO Libarary ( ISBN, [Name], Gategory, Author, Cost, [Date]) " +
          "VALUES ( @ISBN, @Name, @Gategory, @Author, @Cost, @Date) ";
    cmd.Parameters.AddWithValue("@ISBN", ISBNTB.Text);
    cmd.Parameters.AddWithValue("@Name", NameTB.Text);
    cmd.Parameters.AddWithValue("@Gategory", GategoryTB.Text);
    cmd.Parameters.AddWithValue("@Author", AuthorTB.Text);
    cmd.Parameters.AddWithValue("@Cost", int.Parse(CostTB.Text));
    cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Text);



    cmd.ExecuteNonQuery();

        MessageBox.Show("Book Added!");
        conn.Close();


}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

}

private void sellbt_Click(object sender, EventArgs e)
{
     OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\me\Library Store\Library Store\Store.accdb");
     try
{
    conn.Open();

    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = conn;
    cmd.CommandText = " DELETE * FROM Libarary WHERE ISBN=@ISBN AND [Name]=@Name AND Gategory=@Gategory AND Author=@Author AND Cost=@Cost AND [Date]=@Date ";
    cmd.Parameters.AddWithValue("@ISBN", ISBNTB.Text);
    cmd.Parameters.AddWithValue("@Name", NameTB.Text);
    cmd.Parameters.AddWithValue("@Gategory", GategoryTB.Text);
    cmd.Parameters.AddWithValue("@Author", AuthorTB.Text);
    cmd.Parameters.AddWithValue("@Cost", CostTB.Text);
    cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Text);

    cmd.ExecuteNonQuery();

        MessageBox.Show("Book removed to be sold!");
        conn.Close();


}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

} Errow with the record which i try to delete 在此处输入图片说明

database records 在此处输入图片说明

You are facing this error because one/many parameters that you are passing to your query are of not the same type as it is in the database. Cross check them. and ideally should pass parameters to your query like this

cmd.Parameters.Add("@Date", OleDbType.Date); //note i have specified the db type
cmd.Parameters["@Date"].Value =dateTimePicker1.Value;

this will ensure that you have same types as defined in your database

Try:

cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Value);

DateTimePicker.Text returns string representation of selected value, not the value itself.

怎么样?

cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Value.ToString("dd-MM-yyyy"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM