Combobox与表列值 [英] Combobox with a Table Column Values
问题描述
public partial class New_Quote:表格
{
public string busnamevar = ;
public New_Quote()
{
InitializeComponent();
st = value;
FillCombo();
}
void FillCombo()
{
string sql = 从new_customer中选择cusname,其中busname =' + st + ';
// **** string sql =从new_customer中选择cusname,其中busname ='+ busnamevar + ;
MySqlDataReader阅读器;
尝试
{
con = new MySqlConnection(str);
con.Open();
MySqlCommand cmd = new MySqlCommand(sql,con);
reader = cmd.ExecuteReader();
while (reader.Read())
{
string scname = reader.GetString( cusname);
comboBox1.Items.Add(scname);
}
}
catch (MySqlException ex)
{
MessageBox.Show(ex.ToString( ));
}
}
如果我在where子句中使用busnamest中指定的值那么它工作正常,但如果我使用公共字符串变量busnamevar,则comobobox不包含表列中的值。
我试图将公共字符串复制到本地变量但是它不起作用
你的吗?如果总线名称为空,则select语句返回值???
您设置busnamevar = ;
并且您没有为变量指定任何名称。请指定一些名称并查看其是否有效
已编辑:由于OP提到他正在传递另一种形式的价值
选中此项以了解如何在表格之间传递值
[ ^ ]
希望它有所帮助
更新:
而不是将变量busnamevar声明为字符串varibale,将其声明为属性
并在设置值后调用方法Fillcombo < br $> b $ b
private string _busnamevar = string .Empty;
public string Busnamevar
{
get
{
return _busnamevar;
}
set
{
_busnamevar = value ;
FillCombo();
}
}
查询应为
< pre lang =c#> string sql = 选择来自new_customer的cusname,其中busname =' + Busnamevar + ';
public partial class New_Quote : Form
{
public string busnamevar = "";
public New_Quote()
{
InitializeComponent();
st = "value";
FillCombo();
}
void FillCombo()
{
string sql = "select cusname from new_customer where busname='"+st+"'";
//**** string sql = "select cusname from new_customer where busname='"+busnamevar+"'";
MySqlDataReader reader;
try
{
con = new MySqlConnection(str);
con.Open();
MySqlCommand cmd = new MySqlCommand(sql, con);
reader = cmd.ExecuteReader();
while (reader.Read())
{
string scname = reader.GetString("cusname");
comboBox1.Items.Add(scname);
}
}
catch (MySqlException ex)
{
MessageBox.Show(ex.ToString());
}
}
If i use busname in where clause the value specified in the "st" then it is working fine but if I use the "public string variable busnamevar" then comobobox not contains the values from the table column.
I tried to copy the public string to local variable but it is not working
does your select statement returns values if the busname is empty???
you setbusnamevar = "";
and you are not assigning any name for the variable.Assign some name and see whether it works or not
Edited: As the OP mentioned that he is passing value from another form
Check this to know how to pass values between the forms
passing values between two windows forms[^]
Hope it helps
Update:
Instead of declaring the variable busnamevar as a string varibale,declare it as a property
and call the method Fillcombo once the value is set
private string _busnamevar = string.Empty; public string Busnamevar { get { return _busnamevar; } set { _busnamevar = value; FillCombo(); } }
The query should be
string sql = "select cusname from new_customer where busname='"+Busnamevar+"'";
这篇关于Combobox与表列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!