Data Type Mismatch error in Criteria expression in Select query C# query

My sample code is as follows, I am getting following error;

Data Type Mismatch error in criteria expression.

Details => ScannerAlarmLimits is my table from .mdb database.

string jointS = dsetChamberS1.Tables[0].Rows[tot][0].ToString();
int select1S = Convert.ToInt32(jointS);
string sqlQuery1S = "SELECT TMin,TMax,HMin,HMax from ScannerAlarmLimits WHERE ScannerID='" +select1S+ "'";
OleDbCommand cmd1S = new OleDbCommand(sqlQuery1S, conn);
OleDbDataAdapter adapter1S = new OleDbDataAdapter(cmd1S);
adapter1S.Fill(dsetTempS, "ScannerAlarmLimits");

Upvotes: 0

Views: 1053

Answers (2)

Rajdeep
Rajdeep

Reputation: 802

I just added single quote in the condition of where clause, now its working.

var query = "SELECT * from  checkinout where read <> '1'";

Upvotes: 1

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98868

If your ScannerID column is integer, then you should not use single quotes with it. Single quotes are for characters. Like;

WHERE ScannerID = " + select1S;

But as a better way, you should always use parameterized queries. This kind of string concatenations are open for SQL Injection attacks. Aka bobby-tables.

And use using statement to dispose your connections, commands and adapters.

string jointS = dsetChamberS1.Tables[0].Rows[tot][0].ToString();
int select1S = Convert.ToInt32(jointS);
using(var conn = new OleDbConnection(conString))
using(var cmd1S = conn.CreateCommand())
{
    cmd1S.CommandText = "SELECT TMin,TMax,HMin,HMax from ScannerAlarmLimits WHERE ScannerID = @id";
    cmd1S.Parameters.AddWithValue("@id", OleDbType.Integer).Value = select1S;

    using(var adapter1S = new OleDbDataAdapter(cmd1S))
    {
        adapter1S.Fill(dsetTempS, "ScannerAlarmLimits");
    }
}

Upvotes: 1

Related Questions