如何根据DB中的其他Combobox填充Combobox? [英] How to fill a Combobox based on other Combobox from DB?

查看:84
本文介绍了如何根据DB中的其他Combobox填充Combobox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在表单上加载

private void Stock_Load(object sender, EventArgs e)
       {
           this.ControlBox = false;

           try
           {

               string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\WStoreSystem\\WStoreSystem\\WStoreSystem\\bin\\Debug\\StoreSys.mdb";
               OleDbConnection con = new OleDbConnection(str);
               con.Open();
               OleDbCommand comm = new OleDbCommand();
               comm.Connection = con;
               string qu = "SELECT ProName FROM Products";
               comm.CommandText = qu;
               OleDbDataReader reder = comm.ExecuteReader();
               while (reder.Read())
               {
                   comboBox1.Items.Add(reder["ProName"].ToString());
               }
               con.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show("Error " + ex);
           }

       }

,它的工作我得到的comboBox1充满了我需要的数据,当我从列表中选择任何名称时我需要什么在combobox1上,为了在comboBox2上显示另一个相关的字段值,我在comboBox1上的comboBox1_SelectedIndexChanged事件中创建了这个代码



, its working i got the comboBox1 filled with the data i need, what i need when i select any name from the list on combobox1, to display another related field value on the comboBox2, i made this code on comboBox1 at comboBox1_SelectedIndexChanged event

try
           {

               string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\WStoreSystem\\WStoreSystem\\WStoreSystem\\bin\\Debug\\StoreSys.mdb";
               OleDbConnection con = new OleDbConnection(str);
               con.Open();
               OleDbCommand comm = new OleDbCommand();
               comm.Connection = con;
               string qu = "SELECT ProSize FROM Products where ProName like '" + comboBox1.Text + "'";
               comm.CommandText = qu;
               OleDbDataReader reder = comm.ExecuteReader();
               while (reder.Read())
               {
                   comboBox2.Items.Add(reder["ProSize"].ToString());
               }
               con.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show("Error " + ex);
           }







错误是:奇怪的消息不喜欢当我面对错误之前,它提到没有给参数赋值...




the error is: strange message not like when i face an error before, it mentions that no value given to a parameters...

推荐答案

comboBox2.Items.Clear();

尝试

{



string str =Provider = Microsoft.Jet.OLEDB.4.0;数据源= D:\\ WStoreSystem \\ WStoreSystem \\ WStoreSystem \\\\\\Debug \\StoreSys.mdb;

OleDbConnection con = new OleDbConnection(str);

con.Open();

OleDbCommand comm = new OleDbCommand();

comm.Connection = con;

string qu =SELECT ProductID,ProType FROM ProductsData,其中ProName喜欢'+ comboBox1.Text +';

comm.CommandText = qu;

OleDbDataReade r reder = comm.ExecuteReader();

//textProdID.Text = qu.ProductID;

while(reder.Read())

{



comboBox2.Items.Add(reder [ProType]。ToString());



}

con.Close();

}

catch(例外情况)

{

MessageBox.Show(错误+ ex);

}



}



private void comboBox2_SelectedIndexChanged(object sender,EventArgs e)

{

comboBox3.Items.Clear();



尝试

{



string str =Provider = Microsoft.Jet.OLEDB.4.0;数据源= D:\\ WStoreSystem \\ WStoreSystem \\\ \\ WStoreSystem \\\\\\Debug \\StoreSys.mdb;

OleDbConnection con = new OleDbConnection(str);

con.Open( );

OleDbCommand comm = new OleDbCommand();

comm.Connection = con;

string qu =SELECT ProSize FROM ProductsData其中ProType喜欢'+ comboBox2.Text +';

comm.CommandText = qu;

OleDbDataReader reder = comm.ExecuteReader();

while(reder.Read())

{

comboBox3.Items.Add(reder [ProSize]。ToString());

}

con.Close();

}

catch(例外情况)

{

MessageBox.Show(错误+ ex);

}

}
comboBox2.Items.Clear();
try
{

string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\WStoreSystem\\WStoreSystem\\WStoreSystem\\bin\\Debug\\StoreSys.mdb";
OleDbConnection con = new OleDbConnection(str);
con.Open();
OleDbCommand comm = new OleDbCommand();
comm.Connection = con;
string qu = "SELECT ProductID, ProType FROM ProductsData where ProName like '" + comboBox1.Text + "'";
comm.CommandText = qu;
OleDbDataReader reder = comm.ExecuteReader();
//textProdID.Text = qu.ProductID;
while (reder.Read())
{

comboBox2.Items.Add(reder["ProType"].ToString());

}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}

}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox3.Items.Clear();

try
{

string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\WStoreSystem\\WStoreSystem\\WStoreSystem\\bin\\Debug\\StoreSys.mdb";
OleDbConnection con = new OleDbConnection(str);
con.Open();
OleDbCommand comm = new OleDbCommand();
comm.Connection = con;
string qu = "SELECT ProSize FROM ProductsData where ProType like '" + comboBox2.Text + "'";
comm.CommandText = qu;
OleDbDataReader reder = comm.ExecuteReader();
while (reder.Read())
{
comboBox3.Items.Add(reder["ProSize"].ToString());
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}


这篇关于如何根据DB中的其他Combobox填充Combobox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆