Click here to Skip to main content
14,971,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I am able to fetch the data from SQL for the first comboBox based on the radioButton selected. But I want to fetch data from SQL based on the ItemSelected in the comboBox1 to comboBox2.

Please help me in this.

C#
private void ShiftOne_CheckedChanged(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection();
    String connection = con.ConnectionString = "Data Source=MD;Initial Catalog=Locatify;Integrated Security=True";
    con.Open();

        string query = "select distinct departmentone from shiftone";
        SqlCommand cmd = new SqlCommand(query, con);

        using (SqlDataReader saReader = cmd.ExecuteReader())
        {
            while (saReader.Read())
            {
                string name = saReader.GetString(0);
                Department.Items.Add(name);
            }
            con.Close();
        }
}

private void Department_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection();
    String connection = con.ConnectionString = "Data Source=MD;Initial Catalog=Locatify;Integrated Security=True";
    con.Open();

    string staff = "select distinct fnameone from shiftone where departmentone=@department";

    string department = Department.SelectedItem.ToString();

    SqlCommand cmd = new SqlCommand(staff, con);

    SqlDataReader saReader = cmd.ExecuteReader(); //Exception is caught here!

        while (saReader.Read())
        {
            string name = saReader.GetString(0);
            StaffName.Items.Add(name);
        }
        con.Close();


}
Posted
Updated 3-Jan-15 0:03am
v2

1 solution

C#
string staff = "select distinct fnameone from shiftone where departmentone=@department";
string department = Department.SelectedItem.ToString();
SqlCommand cmd = new SqlCommand(staff, con);
//you forgot to set the parameter and the value
cmd.Parameters.AddWithValue("@department", department);
//remove the existing items before adding new items 
StaffName.Items.Clear(); 
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900