I have made a window application using Visual Studio2008,C#,MS Access database.
In my application there are three tables "country","state","person" .On the form,there are 2 comboboxes one for "country" and other is for selecting "state" , also a textbox for entering the name of a person for each country-state pair.
Now I want that when I select any particular country from combobox1, then the corresponding states should display in combobox2 (not all states) i.e value of combobox2 depends upon the value of combobox1. Now for this country-state pair I have add the name of a person to the database.

For each combobox I have set the dataset, Display source, display member and value member in design view.

And for filling values in two combobox ,I have made two functions - "country_combo()" and "state_combo()". The code is as follows:

private void country_Combo()
        {
             cls_Con.comboFill(this.com_Country, "select * from country", "country", "c_name", "c_id");
            
        }
        private void state_Combo()
        {
            cls_Con.comboFill(this.com_State, "select * from [state] ", "[state]", "st_name", "st_id");
        }

But my code is not working , its displaying all the states .
How can I load only those states(in combobox2) that corresponds to the country that is selected (in combobox1).:pretty:

>How can I load only those states(in combobox2) that corresponds to the country that is selected (in combobox1).

SELECT ... WHERE clause.

select [states] from tableName where [country]=@country

>How can I load only those states(in combobox2) that corresponds to the country that is selected (in combobox1).

SELECT ... WHERE clause.

select [states] from tableName where [country]=@country

I am using the following code:

cls_Con.comboFill(this.com_Section, "select sec_name from [section] where com_Class=@c_name", "[section]", "sec_name", "sec_id");

Its giving error "NO VALUE GIVEN FOR ONE OR MORE REQUIRED PARAMETERS"

>Its giving error "NO VALUE GIVEN FOR ONE OR MORE REQUIRED PARAMETERS

You need to set parameter value. It is a good choice but you can use following solution:

private void state_Combo(string country)
        {
            cls_Con.comboFill(this.com_State, "select * from [state] where [country]='" + this.com_Country.SelectValue + "'", "[state]", "st_name", "st_id");
        }

Again I am getting same error and the code which I used is :

private void country_Combo()
        {
             cls_Con.comboFill(this.com_Country, "select * from country", "country", "c_name", "c_id");
           
        }

        private void state_Combo()
        {
             cls_Con.comboFill(this.com_State, "select st_name from [state] where [B][country][/B]='" + com_Country.SelectedValue.ToString() + "'", "[state]", "st_name", "st_id"););     
        }

And when I am using the following code(shown in bold):

private void state_Combo()
        {
             cls_Con.comboFill(this.com_State, "select st_name from [state] where [B]c_name[/B]='" + com_Country.SelectedValue.ToString() + "'", "[state]", "st_name", "st_id"););     
        }

In this case no value is displaying in combox2

I tried the following code, but I am getting black combobox:

public void state_Combo(string c_name)
        {
            
           try
                {
                    cls_Con.cmdOpen(cmdForm);

            cls_Con.comboFill(this.com_State, "select * from [state] where c_name='" + com_Class.SelectedItem.ToString() + "'", "[state]", "st_name", "st_id");
            cls_Con.cmdClose(cmdForm);
                }
            catch (Exception exp)
            {
                clsmyFunction.setCreateError(exp.Message);

            }
            finally
            {
                cls_Con.cn.Close();
            }
            
        }

IN this code, I ma neither getting error nor any data in state combobox. state combobox is having no values.

KIndly provide some solution

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.