populate two dropdownlist

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 40
Reputation: raghu.8 is an unknown quantity at this point 
Solved Threads: 0
raghu.8 raghu.8 is offline Offline
Light Poster

populate two dropdownlist

 
0
  #1
Nov 30th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: populate two dropdownlist

 
0
  #2
Nov 30th, 2007
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 ?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 40
Reputation: raghu.8 is an unknown quantity at this point 
Solved Threads: 0
raghu.8 raghu.8 is offline Offline
Light Poster

Re: populate two dropdownlist

 
0
  #3
Dec 1st, 2007
thank you or ypur advice
but i am developing web application using C#,just look at my code which is giving error message...
  1. if (Page.IsPostBack == false)
  2. {
  3. SqlConnection con = new SqlConnection("user id=sa;password=vubrain;database=raghu;data source=vubrain4");
  4. SqlDataAdapter da = new SqlDataAdapter("select countyr_id,countryname from country_master", con);
  5. DataSet ds1 = new DataSet();
  6. da.Fill(ds1, "country_master");
  7. dd1.DataSource = ds1.Tables["country_master"];
  8. dd1.DataTextField = "countryname";
  9. dd1.DataValueField = "countyr_id";
  10. dd1.DataBind();
  11. }
  12. }
  13. protected void dd1_SelectedIndexChanged(object sender, EventArgs e)
  14. {
  15. string countryid;
  16. countryid = dd1.SelectedValue;
  17. SqlConnection con = new SqlConnection("user id=sa;password=vubrain;database=raghu;data source=vubrain4");
  18. SqlDataAdapter da1 = new SqlDataAdapter("SELECT stateid,statename FROM countrystate");
  19. DataSet ds1 = new DataSet();
  20. da1.Fill(ds1, "countrystate");
  21. dd2.DataSource = ds1.Tables["countrystate"];
  22. dd2.DataTextField = "statename";
  23. dd2.DataValueField = "stateid";
  24. dd2.DataBind();
  25.  
  26.  
  27. }
  28. protected void dd2_SelectedIndexChanged(object sender, EventArgs e)
  29. {
  30. string stateid;
  31. stateid = dd2.SelectedValue;
  32.  
  33. }
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...
Last edited by cscgal; Dec 4th, 2007 at 3:07 am. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 40
Reputation: raghu.8 is an unknown quantity at this point 
Solved Threads: 0
raghu.8 raghu.8 is offline Offline
Light Poster

Re: populate two dropdownlist

 
0
  #4
Dec 1st, 2007
how many tables should we have to create
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: populate two dropdownlist

 
0
  #5
Dec 1st, 2007
You need to specify what country to use when you pull the data for the second drop down.

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

This way it will only contain the states for that country.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 40
Reputation: raghu.8 is an unknown quantity at this point 
Solved Threads: 0
raghu.8 raghu.8 is offline Offline
Light Poster

Re: populate two dropdownlist

 
0
  #6
Dec 3rd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: populate two dropdownlist

 
0
  #7
Dec 3rd, 2007
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.

  1. protected void dd1_SelectedIndexChanged(object sender, EventArgs e)
  2. {
  3. string countryid;
  4. countryid = dd1.SelectedValue;
  5. SqlConnection con = new SqlConnection("user id=sa;password=vubrain;database=raghu;data source=vubrain4");
  6. SqlDataAdapter da1 = new SqlDataAdapter(string.Format("SELECT stateid,statename FROM countrystate where country_id='{0}' ",countryid) );
  7. DataSet ds1 = new DataSet();
  8. da1.Fill(ds1, "countrystate");
  9. dd2.DataSource = ds1.Tables["countrystate"];
  10. dd2.DataTextField = "statename";
  11. dd2.DataValueField = "stateid";
  12. dd2.DataBind();
  13. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 40
Reputation: raghu.8 is an unknown quantity at this point 
Solved Threads: 0
raghu.8 raghu.8 is offline Offline
Light Poster

Re: populate two dropdownlist

 
0
  #8
Dec 4th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: populate two dropdownlist

 
0
  #9
Dec 4th, 2007
ok, I will write the code example, and post it here in the next few minutes.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: populate two dropdownlist

 
0
  #10
Dec 4th, 2007
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
Attached Files
File Type: zip TwoDropDown.zip (32.9 KB, 105 views)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC