Click here to Skip to main content
15,745,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table in access db that has 3 cloumns- Guest_id(Number), Guest_name(Text), Rooms(Text). I get an error on the 5th line when i execute the code saying Data type mismatch in criteria expression . I dont know what the problem is. Please help. Thanks in advance.

cmd.Connection = cn;
query = "Select * from Guest where Guest_id = '"+Convert.ToInt32(guestid_comboBox.Text)+"'";
cmd = new OleDbCommand(query, cn);
dr = cmd.ExecuteReader();
if (dr.Read())
   guestname_textBox.Text = dr["Guest_name"].ToString();
Updated 14-Aug-11 1:40am

1 solution

Use the debugger to see what's the value of guestid_comboBox.Text. Most likely you're trying to convert text to a number.

Another thing. Instead of concatenating values directly to an SQL statement, use bind variables instead. See: OleDbParameter[^]
Share this answer
Bennet Ryan 14-Aug-11 7:55am    
the value of guestid_comboBox.Text is a number. sorry i didn't understand the material that as there in the link, i'm new to If you kindly help me my tell the code to me please. Thank you for your help...
Wendelius 14-Aug-11 8:33am    
Ok, the next thing is, is Guest_id numerical or character in the database? If it's numerical, remove the apostrophes:
query = "Select * from Guest where Guest_id = "+Convert.ToInt32(guestid_comboBox.Text);

Bind variables are used instead of literal values in SQL statements, for example typically you don't define a query like
... WHERE SomeField = " + numberVariable;
Instead you should use
... WHERE SomeField = :numberVariable";
and later define the parameter :numberVariable and give it a value. This saves from data type mismatches, sql injections etc.

You can go through the examples in MSDN to get a clear picture. Also here's one good link:[^]. That's for different classes (SqlParameter) but the basic idea is the same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900