简体   繁体   中英

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 am 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 :-)

:-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM