Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.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# 根据列表框值通过数据库为标签赋值_C#_.net_Sql_Listbox - Fatal编程技术网

C# 根据列表框值通过数据库为标签赋值

C# 根据列表框值通过数据库为标签赋值,c#,.net,sql,listbox,C#,.net,Sql,Listbox,我想从数据库中为列表框中选择的值分配五个标签的值。 db查询返回包含多条记录的单个列。 请帮忙。 我与C#2010和MS SQL合作 我目前的代码是: private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { try { String c1, c2; c1 = "NULL"; MessageBox.Show("LB index :"+listBo

我想从数据库中为列表框中选择的值分配五个标签的值。
db查询返回包含多条记录的单个列。 请帮忙。
我与C#2010和MS SQL合作

我目前的代码是:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        String c1, c2;
        c1 = "NULL";
        MessageBox.Show("LB index :"+listBox1.SelectedIndex.ToString());
        //p = listBox1.SelectedItem.ToString();
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=localhost;Initial Catalog=eVoting;Integrated Security=True;Pooling=False";
        con.Open();
        MessageBox.Show("List bOx sect  :"+listBox1.SelectedValue.ToString());
        SqlCommand cmd = new SqlCommand("select Firstname from candidates where position ='" + listBox1.SelectedValue.ToString() + "'", con);
        int index = 0;
        SqlDataReader reader = cmd.ExecuteReader();
        while(reader.Read())
        {
            if (index == 0)
            {
                c1 = reader[index].ToString();
                radioButton1.Text = c1;
            }
            if (index == 1)
            {
                c1 = reader[index].ToString();
                radioButton2.Text = c1;
            }
            if (index == 2)
            {
                c1 = reader[index].ToString();
                radioButton3.Text = c1;
            }
            if (index == 3)
            {
                c1 = reader[index].ToString();
                radioButton4.Text = c1;
            }
            if (index == 4)
            {
                c1 = reader[index].ToString();
                radioButton4.Text = c1;
            }
            if (index == 5)
            {
                c1 = reader[index].ToString();
                radioButton5.Text = c1;
            }
            MessageBox.Show("c1  :" + c1);
            index++;
        }

    }
    catch (Exception E)
    {
    }
}
制造

DataTable dt=newdatatable();
dt.负载(读卡器);
对于(int i=0;i
您的代码是正确的我从您的查询中得到了多少信息,调试得越多越好。查询是正确的…但是执行first()后while循环停止如果read stmt将在一次尝试中读取整个大小写,您需要读取一行。ReadLine()函数不可用..:(我发现答案是我自己的问题是,它返回单列,我给索引值0到5,我只是改变了它,它工作得很顺利
DataTable dt=new DataTable();
        dt.Load(reader);
        for (int i = 0; i < dt.Rows.Count;i++ )
        {
            {
                if (i == 0)
                {
                    c1 = dt.Rows[i]["Firstname "].ToString();
                    radioButton1.Text = c1;
                }
                if (index == 1)
                {
                    c1 = dt.Rows[i]["Firstname "].ToString();
                    radioButton2.Text = c1;
                }
                ..
                MessageBox.Show("c1  :" + c1);

            }