Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have two table ,
first table Like this :- city_id , City_Name .... city_id is PK
------ , ---------
1 , city1
2 , city2

table 2 : - City_id , center_id , Center_Name ..... City_id is FK, Center_id is PK
-------- , ---------- , -----------
1 , 1 , center1
1 , 2 , center2
2 , 3 , center3
2 , 4 , center4

i have two combobox ,the first to display City_Name ,the second to dispaly center_Name when the user choose City_NAme fro first combobox .

this is my code :-

C#
void FillComboCity()
        {          
            try
            {
                dSet = new DataSet();
                con.Open();
                s = "select City_id , City_Name from City";
                sCommand = new SqlCommand(s, con);
                sdAdapter = new SqlDataAdapter();
                sdAdapter.SelectCommand = sCommand;
                sdAdapter.Fill(dSet);
                DataRow dr = dSet.Tables[0].NewRow();
                dr.ItemArray = new object[2] { 0, " ---Select--- " };
                dSet.Tables[0].Rows.InsertAt(dr, 0);
                CB_City.ValueMember = "City_id";
                CB_City.DisplayMember = "City_Name";
                CB_City.DataSource = dSet.Tables[0];
                con.Close();
            }
            catch
            {
                return;
            }
        }

        void FillComboCenter()
        {
            try
            {
                dSet = new DataSet();
                con.Open();
                s = "select Center_id , Center_Name from Center where City_id = '"+CB_City.Text+"'";
                sCommand = new SqlCommand(s, con);
                sdAdapter = new SqlDataAdapter();
                sdAdapter.SelectCommand = sCommand;
                sdAdapter.Fill(dSet);
                DataRow dr = dSet.Tables[0].NewRow();
                dr.ItemArray = new object[2] { 0, " ---Select--- " };
                dSet.Tables[0].Rows.InsertAt(dr, 0);
                CB_Center.ValueMember = "Center_id";
                CB_Center.DisplayMember = "Center_Name";
                CB_Center.DataSource = dSet.Tables[0];
                con.Close();
            }
            catch
            {
                return;
            }
        }
 private void FRM_PatientData_Load(object sender, EventArgs e)
        {
            
                FillComboCity();
                FillComboCenter(); 
        }


What I have tried:

How i display data in combobox?
Posted
Comments
Sinisa Hajnal 11-Feb-16 7:40am    
So, you didn't actually try anything? Google? Check code project/stack overflow/MSDN?
You fill both comboboxes, but with second one either empty or with centers from first city selected in first combobox (unless you're showing IDs in that combobox, I'd say you get nothing in the second box).

You have to describe your problem, describe what you've tried and what error do you get.
Richard Deeming 11-Feb-16 8:27am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
RDBurmon 11-Feb-16 9:07am    
execute the FillComboCenter() function on SelectedTextIndexChanged event
See this -
https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged(v=vs.110).aspx

1 solution

Tip 1 -
execute the FillComboCenter() function on SelectedTextIndexChanged event
See this -
https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged(v=vs.110).aspx

Tip 2 -
See my answer here if you really need a code. Below code is not SQL query but you can refer the logic and create the new one as per your requirement
https://www.codeproject.com/Answers/1077013/I-have-doubt-how-to-populate-one-dropdown-list-box[^]
 
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