开发者

Reading columns using WHERE clause on MS Access passing combobox textvalue

开发者 https://www.devze.com 2023-03-15 18:54 出处:网络
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

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

:-)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号