Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
On my Windows form, there are two comboboxes showing name of specific courses and number of sections respectively. But, I want to write some codes for my combobox (number of sections) that makes it enable to choose the correct section number automatically. For example, for course C#, if section one is inserted into SQL Server, the combobox for section number should select section two for that course.
C#
SqlConnection objConnection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\UniversityDataBase.mdf;Integrated Security=True");

private void CourseName_Click(object sender, EventArgs e)
{
    string query = "SELECT *FROM CourseTable";

    SqlDataAdapter SDA = new SqlDataAdapter(query, objConnection);
    DataTable dt = new DataTable();
    SDA.Fill(dt);

    comboBox1.DataSource = dt;
    comboBox1.DisplayMember = "Coursename";
    objConnection.Close();
}

private void SectionNo_Click(object sender, EventArgs e)
{
    string query = "SELECT * FROM SecNo";

    SqlDataAdapter SDA = new SqlDataAdapter(query, objConnection);
    DataTable dt = new DataTable();
    SDA.Fill(dt);

    comboBox2.DataSource = dt;
    comboBox2.DisplayMember = "SectoinNumber";

    objConnection.Close();
}


What I have tried:

c# - Select Next Item from Combo Box if the item exists in SQL Server database - Stack Overflow[^]
Posted
Updated 12-Feb-18 21:31pm
v2
Comments
#realJSOP 12-Feb-18 7:20am    
Nobody can provide an answer based on the info you provided

1 solution

this can be your solution

1) you need to write code logic on selection of combobox one event
2) in the code logic - first run query to get the section inserted for that selected course
3) then update the property of second combox box to select the section (which will be above section number + 1)
 
Share this answer
 

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