Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么名称不显示在基于组合框选定id的文本框中_C# - Fatal编程技术网

C# 为什么名称不显示在基于组合框选定id的文本框中

C# 为什么名称不显示在基于组合框选定id的文本框中,c#,C#,我正在使用下面的代码,但它并没有在文本框中显示名称\ private void AllotLeaves_Load(object sender, EventArgs e) { display(); } private void display() { SqlConnection conn = new SqlConnection(@"Data Source=......."); conn.Open();

我正在使用下面的代码,但它并没有在文本框中显示名称\

 private void AllotLeaves_Load(object sender, EventArgs e)
    {
        display();
    }
    private void display()
    {
        SqlConnection conn = new SqlConnection(@"Data Source=.......");
        conn.Open();
        SqlDataAdapter ad = new SqlDataAdapter("select *from Emp_Details", conn);
        DataSet ds = new DataSet();
        ad.Fill(ds, "Emp_Details");
        cballotid.DataSource = ds.Tables["Emp_Details"].DefaultView;
        cballotid.DisplayMember = "ID";
        cballotid.ValueMember = "ID";

    }
    private void cballotid_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"Data Source=......");
        conn.Open();
        SqlCommand command = new SqlCommand("select Name from Emp_Details where ID=@ID", conn);
        command.Parameters.AddWithValue("@ID", cballotid.Text);
        Object temp = command.ExecuteScalar();  // this returns the first value of the select statement.
        if (temp != null)
            txtallotname.Text = temp.ToString();
        conn.Close();
    }

你必须改变这个

cballotid.DisplayMember = "ID";

cballotid.ValueMember = "ID";


将此行更改如下:-

command.Parameters.AddWithValue("@ID", cballotid.SelectedValue);
试试这个

  cballotid.DisplayMember = "Name";
  cballotid.ValueMember = "ID";
  cballotid.BindingContext = this.BindingContext;

参考资料:

是否为温度…空值?在那里放置一个断点…代码似乎很好,如果调试并查看,您应该能够找到解决方案
  cballotid.DisplayMember = "Name";
  cballotid.ValueMember = "ID";
  cballotid.BindingContext = this.BindingContext;