Search code examples
c#ms-accessbetweenunhandled-exceptionmismatch

Error: Data type mismatch in criteria expression


I've made a program about flights using C# linked to an Access DB (for school).
However, after doing all the basics, I wanted to score some extra points.

My DB consists of two tables: reizigers (passengers) and vluchten (flights). All the classes and variables should be defined correctly.

My problem: I want to be able to search for flights between certain cost ranges.

However, after executing the query and thus trying it, it gives the error:

Additional information: Data type mismatch in criteria expression.

In my DB, the cost is kost, and it's just a single Integer (e.g., 240).

Does anybody have any idea what it might be?

repository wpf window, entry


Solution

  • KistMin and KostMax are two integers and I suppose that also the datatable field Kost, used in your WHERE clause, is of type integer.

    But your query encloses the two variables between single quotes and this transform them in strings. The database doesn't like to compare an integer field containing a numeric value against a text (also if this text is composed of digits) and complains with the mentioned error.

    A simple fix is removing the quotes around the two values that you put in your sql where condition and fixing the syntax of the BETWEEN predicate. (Not sure what actually that syntax produce, probably a boolean result evaluated against the Kost field, not what you expect)

    ".... WHERE Kost BETWEEN " + KostMin + " AND " + KostMax;
    

    In your context (MS-Access, using integers) there is no concern for Sql Injection, but it is a good practice start using parameterized queries.