对不起,我的英文不好。我昨天晚上写了代码。我的程序运行成功,但fill_model的答案不正确。它应该是不同型号的领域,但它显示了与特定品牌相关的所有型号。你有什么主意吗? 这里是代码:ComboBox未填充所需值
private void brand_SelectedIndexChanged(object sender, EventArgs e)
{
fill_brand_txt();
fill_model();
model.Text = "";
}
private void fill_brand_txt()
{
string cs = "Data Source=192.168.1.20;Initial Catalog=Test;User ID=sa;Password=Abc123;";
string qu = "select id_kind from model where [email protected]";
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand(qu, con);
cmd.Parameters.AddWithValue("@brand1", brand.Text);
try
{
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
rd.Read();
brand_txt.Text = rd["id_kind"].ToString();
}
catch (Exception ex)
{
MessageBox.Show("Error :", ex.Message);
}
finally
{
con.Close();
}
}
private void fill_model()
{
string cs = "Data Source=192.168.1.20;Initial Catalog=Test;User ID=sa;Password=Abc123;";
string que = "select model_name from model where [email protected]";
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand(que, con);
cmd.Parameters.AddWithValue("@id", brand_txt.Text);
con.Open();
System.Data.DataTable dt = new System.Data.DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
model.DisplayMember = "model_name";
model.DataSource = dt;
con.Close();
}
你的意思是使用brand_txt.Text'的'价值还是应一个不同的控件来读取值?顺便说一句,你应该在'con.Close()'后面调用'con.Dispose()'。 – 2014-09-13 15:14:37
'id_kind'是你品牌的ID吗? – 2014-09-13 15:19:10
fill_brand_txt中的brand_txt值更改是否成功? sql参数是否在fill_model中获得所需的值?你调试了吗? – Biri 2014-09-13 15:22:55