Search code examples
c#sqlms-accessoledbcommand

OleDb.OleDbException Data type mismatch in criteria expression


I want to have a query like this but I get errors:

errors

"Select * 
 From Products 
 Where (ProductName)  = '" + productName.Text + "' and  (Revise) = '" + a + "'";

where I have product name and revise at the same table as 2 different columns.

I have several rows with same product name but different revise dates. I am trying the pick the correct one.

con.Open();

OleDbCommand select = new OleDbCommand();
select.Connection = con;

Object revize = revizyonlar.SelectedItem;

if(revize != null)
{
    string a = revizyonlar.SelectedItem.ToString();
    System.DateTime b = Convert.ToDateTime(a);
    a = b.Date.ToShortDateString();
    a = "Select * From Products where (ProductName)  = '" + productName.Text + "' and  (Revise) = '" + a + "'";

    select.CommandText = a;
}
else
    select.CommandText = "Select * From Products Where (ProductName)  = '" + productName.Text + "'";

OleDbDataReader reader = select.ExecuteReader();

while (reader.Read())
{
}

Solution

  • Date expressions are delimited by octothorpes in Access, so:

    a = b.Date.ToString("yyyy'/'MM'/'dd");
    a = "Select * From Products Where ProductName = '" + productName.Text + "' And Revise = #" + a + "#";
    

    That said, do listen to Ňɏssa.