what's the reason for error

Asked By George
02-Sep-10 12:38 PM
Earn up to 0 extra points for answering this tough question.
i have one combo box and one text box in my form.... combo box is attached with access database and i want that whenever i select value from combo box the corresponding value from database should be add in text box...
i am writing code on selectedvalue changed event of combo box1.. whenever i select value from combo box it gives error (Data type mismatch in criteria expression..)
my code is as following:

private void comboBox2_SelectedValueChanged(object sender, EventArgs e)

{

 

OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Harpreet\\Documents\\db4.mdb");

con.Open();

string sql = "select floor from Table1 where RoomNo = '" + comboBox2.Text + "'";

OleDbDataAdapter da = new OleDbDataAdapter(sql, con);

comboBox2.SelectedIndex = 0;

DataSet ds = new DataSet();

da.Fill(ds, "Table1");

 

DataRow drow = ds.Tables["Table1"].Rows[0];

textBox1.Text = drow.ItemArray.GetValue(0).ToString();

con.Close();

}


plz suggest what is the reason behind that error...
i will be very thankful to you....

  re: what's the reason for error

Web Star replied to George
02-Sep-10 12:55 PM
try to put value as string in query as follows

string sql = "select floor from Table1 where RoomNo = '" + comboBox2.Text.ToString() + "'";

  re: what's the reason for error

Web Star replied to George
02-Sep-10 12:57 PM
And also make sure the datatype of RoomNo  in your table should be text or string type.
Create New Account