Assigning values to the lable through database depending on listbox values
- by SurajVitekar
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)
{
}
}