DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C# (http://www.daniweb.com/forums/forum61.html)
-   -   populate two dropdownlist (http://www.daniweb.com/forums/thread99000.html)

raghu.8 Nov 30th, 2007 4:19 am
populate two dropdownlist
 
hi
i want to populate country's name in 1st dropdownlist and their states should be placed in the 2nd dropdownlist, if we select any state it should place the name of the capital in the textbox besides that and so on.....
If possible send me code

thank you in advance
raghu

JerryShaw Nov 30th, 2007 10:56 am
Re: populate two dropdownlist
 
Simply write an event handler on the SelectedIndex change of the combobox to adjust the item for the second. On the second, have another event handler that sets the textbox value. You can populate a Combo using objects that contain the matching information to go into the text box.
Is this a homework assignment ?

raghu.8 Dec 1st, 2007 3:06 am
Re: populate two dropdownlist
 
thank you or ypur advice
but i am developing web application using C#,just look at my code which is giving error message...
        if (Page.IsPostBack == false)
        {
            SqlConnection con = new SqlConnection("user id=sa;password=vubrain;database=raghu;data source=vubrain4");
            SqlDataAdapter da = new SqlDataAdapter("select countyr_id,countryname from country_master", con);
            DataSet ds1 = new DataSet();
            da.Fill(ds1, "country_master");
            dd1.DataSource = ds1.Tables["country_master"];
            dd1.DataTextField = "countryname";
            dd1.DataValueField = "countyr_id";
            dd1.DataBind();
        }
    }
    protected void dd1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string countryid;
        countryid = dd1.SelectedValue;
        SqlConnection con = new SqlConnection("user id=sa;password=vubrain;database=raghu;data source=vubrain4");
        SqlDataAdapter da1 = new SqlDataAdapter("SELECT stateid,statename FROM countrystate");
        DataSet ds1 = new DataSet();
        da1.Fill(ds1, "countrystate");
        dd2.DataSource = ds1.Tables["countrystate"];
        dd2.DataTextField = "statename";
        dd2.DataValueField = "stateid";
        dd2.DataBind();


    }
    protected void dd2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string stateid;
        stateid = dd2.SelectedValue;
       
    }
If i want to select any country from 1st dropdownlist then,2nd dropdownlist should populate with all the state of that paticular country only and if we select any state from 2nd dropdown then the capital should be placed in text box beside that and so on...

raghu.8 Dec 1st, 2007 3:10 am
Re: populate two dropdownlist
 
how many tables should we have to create

JerryShaw Dec 1st, 2007 2:54 pm
Re: populate two dropdownlist
 
You need to specify what country to use when you pull the data for the second drop down.

string sql = "SELECT stateid,statename FROM countrystate where country_id='{0}' ";
SqlDataAdapter da1 = new SqlDataAdapter( string.Format(sql,countryid) );

This way it will only contain the states for that country.

raghu.8 Dec 3rd, 2007 6:06 am
Re: populate two dropdownlist
 
i am no geting in that way
will you please explain what are the table are to be created..
please send posr my reply its very important
thank you in advance

JerryShaw Dec 3rd, 2007 12:10 pm
Re: populate two dropdownlist
 
Your code should work fine as is, you just need to give the Where Country_id= ... in the query so that it returns the states for that country.

protected void dd1_SelectedIndexChanged(object sender, EventArgs e)
{
string countryid;
countryid = dd1.SelectedValue;
SqlConnection con = new SqlConnection("user id=sa;password=vubrain;database=raghu;data source=vubrain4");
SqlDataAdapter da1 = new SqlDataAdapter(string.Format("SELECT stateid,statename FROM countrystate where country_id='{0}' ",countryid) );
DataSet ds1 = new DataSet();
da1.Fill(ds1, "countrystate");
dd2.DataSource = ds1.Tables["countrystate"];
dd2.DataTextField = "statename";
dd2.DataValueField = "stateid";
dd2.DataBind();
}

raghu.8 Dec 4th, 2007 1:04 am
Re: populate two dropdownlist
 
I wrote the code as you said, but it showing two error in sqldataAdapter


1.Argument '1': cannot convert from 'string'to 'System.Data.SqlClient.SqlCommand'

2.The best overloaded method match for 'System.Data.SqlClient.SqlDataAdapter.SqlDataAdapt er

please sought this problem

JerryShaw Dec 4th, 2007 1:23 am
Re: populate two dropdownlist
 
ok, I will write the code example, and post it here in the next few minutes.

JerryShaw Dec 4th, 2007 2:55 am
Re: populate two dropdownlist
 
1 Attachment(s)
Attached is a Windows Application project that demos what you are looking for,
I am using my own server (connection string), and it uses two tables. Read the top of the form.c to see my comments.
You should have no problem converting this to asp.

Good luck,
J


All times are GMT -4. The time now is 3:22 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC