Reading columns using WHERE clause on MS Access passing combobox textvalue
I am working on a windows application written in C# and using MS Access 2003 as my database. I a开发者_运维问答m facing strange problem, when I write the query
SELECT * FROM mytable WHERE columnname='"+combobox.text+"'
and execute using oledbadapter, the dataset visualizer doesn't recognise the where
clause and returns empty table. Whereas
SELECT * FROM mytable WHERE columnname='"+integervalue+"
will return the filtered column.
Here is my code snippet:
private void viewinfo_Click(object sender, EventArgs e){
dataGridView1.Visible = true;
OleDbCommand cmd;
string schoolname = cmbschoolname.Text;
OleDbDataAdapter da = new OleDbDataAdapter("select * from tblmedic where medicalschool ='"+combobox.text+"'", conn);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
output: returns empty table when I pass the text value in the where-clause.
I have resolved this issue. it was the case of empty spaces in my access 2003 table columns. so used a trim() to clear the spaces while selecting the columns in where clause.
private void viewinfo_Click(object sender, EventArgs e){
dataGridView1.Visible = true;
OleDbCommand cmd;
string schoolname = cmbschoolname.Text;
OleDbDataAdapter da = new OleDbDataAdapter("select * from tblmedic where medicalschool ='"+combobox.text.ToString().Trim()+"'", conn);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
Sometimes silly mistakes makes u crave like anything :-)
:-)
精彩评论