使用所选项目从数据库加载文本框更改了组合框c#

时间:2017-04-28 14:16:13

标签: c# visual-studio windows-ce

当我尝试加载表单包含两个组合框和两个文本框时,我有一个例外 我从数据库加载组合框项目,我尝试从表局加载数据并填写两个文本框 图片 : [我得到的例外]

enter image description here

[我有的表格]

enter image description here

public partial class Modifier : Form
{

    public DataSet ds = new DataSet();

    public DataSet ds2 = new DataSet();

    public SqlCeConnection conn = new SqlCeConnection(@"Data Source=\Program Files\projetpfe\Inventaire.sdf");

    public SqlCeDataAdapter da, da2;
    public SqlCeCommand cmd, cmd2;
    public SqlCeDataReader dr,dr2;
    public Modifier()
    {
        InitializeComponent();

        this.da = new SqlCeDataAdapter("Select Code_locale from Bureau", conn);
        this.da2 = new SqlCeDataAdapter("Select DISTINCT Adresse from Bureau ", conn); 
        this.cmd = new SqlCeCommand("Select * from Bureau where Code_locale='" + comboBox1.SelectedValue + "'", conn);

        conn.Open();

    }

    private void Modifier_Load(object sender, EventArgs e)
    {

        this.ds.Tables.Clear();
        this.da.Fill(this.ds, "Bureau");
        comboBox1.DataSource = this.ds.Tables["Bureau"];
        comboBox1.DisplayMember = "Code_locale";
        this.ds2.Tables.Clear();
        this.da2.Fill(this.ds2, "Bureau");
        comboBox2.DataSource = this.ds2.Tables["Bureau"];
        comboBox2.DisplayMember = "Adresse";
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            textBox1.Text = dr[1].ToString();
            textBox2.Text = dr[2].ToString();
        }
    }

1 个答案:

答案 0 :(得分:1)

我解决了我的问题,我使用了选择更改的组合框和我将所有代码复制到组合框选择更改的事件并解决了我的问题,当我选择组合框项目时,文本框显示datareader < / p>