Assigning values to the lable through database depending on listbox values

Posted by SurajVitekar on Stack Overflow See other posts from Stack Overflow or by SurajVitekar
Published on 2013-11-11T09:18:55Z Indexed on 2013/11/11 9:54 UTC
Read the original article Hit count: 176

Filed under:
|
|
|

I want to assign the values of five labels from database regarding to values selected in listbox.
The db query returns single column with multiple records. Please help.
I'm working with C# 2010 and MS SQL.

My current code is:

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)
    {
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET