plz通过组合框解决此错误到搜索数据 [英] plz solve this Error into search data by combobox
本文介绍了plz通过组合框解决此错误到搜索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通过combobox从数据库搜索数据到datagridview
我的表: - uid navarchar(50)[数据类型]
名称Varchar(50)[数据类型] // dispalyname在组合框中
错误: - 列名'mb08'(uid)无效。
private void comboBox2_SelectedIndexChanged( object sender,EventArgs e)
{
string qry = select * from tbmobile其中uid = + comboBox2.SelectedValue;
SqlDataAdapter da = new SqlDataAdapter(qry,con);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Refresh();
}
解决方案
你应该使用 SelectedItem
如此处所述 [ ^ 一>]。您还应该使用正确的参数化查询,尤其是因为您不知道从组合框中返回什么。
在您的代码中尝试以下更改。
< pre lang =c#> string qry = 选择*来自tbmobile,其中uid =' + comboBox2.SelectedValue + ';
希望它能起作用。
Search Data from database by combobox into datagridview
my table:- uid navarchar(50)[Datatype]
Name Varchar(50)[Datatype]// dispalyname in combobox
ERROR:- Invalid column name 'mb08'(uid).
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
string qry = "select * from tbmobile where uid=" + comboBox2.SelectedValue;
SqlDataAdapter da = new SqlDataAdapter( qry,con);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Refresh();
}
解决方案
You should use SelectedItem
as described here[^]. You should also be using proper parameterised queries, especially as you have no idea what will be returned from the combobox.
Try following changes in your code.
string qry = "select * from tbmobile where uid='" + comboBox2.SelectedValue + "'";
Hope it will work.
这篇关于plz通过组合框解决此错误到搜索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文