Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data type mismatch

Tags:

c#

I got this error when I tried to select userid from a database to datatable. The first userid is an autonumber, the second USERID is a number, and the database is a MS Access DB.

private void ()
{
    OdbcDataAdapter ad = new OdbcDataAdapter("select userid from userinfo where BadgeNumber='" + UserID + "'", this.FM.Cn);
    DataTable t = new DataTable();
    ad.Fill(t);
    ad.Dispose();
    if (t.Rows.Count > 0)
    {
        OdbcCommand cmd = new OdbcCommand();
        cmd.Connection = this.FM.Cn;

        string id = t.Rows[0][0].ToString();
        //Check Date
        OdbcDataAdapter add = new OdbcDataAdapter("Select USERID from checkinout where Userid='" + id + "'", this.FM.Cn);
        DataTable tc = new DataTable();
        add.Fill(tc); // <- I gotta error here.
        add.Dispose();
    }
}
like image 962
Ath Piseth Avatar asked Apr 20 '26 03:04

Ath Piseth


1 Answers

Change your query to:

"Select USERID from checkinout where Userid=" + id

In SQL queries, strings literals (or chars) are required to be enclosed within a pair of single quotes ', which are used to delimiter the string. A delimiter is pretty much a character used to identify boundaries - in the case of a string, the single quotes specify where the string starts and where it ends.

Because of the nature of numbers (integers for example), it is not necessary to indicate a delimiter such as single quotes. Your code was failing because when the database engine saw the single quotes, it was expecting a string, but your column was a number datatype, and this is why you obtained a Data type mismatch error when executing your query.

like image 61
Hanlet Escaño Avatar answered Apr 22 '26 17:04

Hanlet Escaño



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!