943,522 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 19033
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 30th, 2007
0

populate two dropdownlist

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
raghu.8 is offline Offline
40 posts
since Nov 2007
Nov 30th, 2007
0

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 ?
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Dec 1st, 2007
0

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...
C# Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
raghu.8 is offline Offline
40 posts
since Nov 2007
Dec 1st, 2007
0

Re: populate two dropdownlist

how many tables should we have to create
Reputation Points: 10
Solved Threads: 0
Light Poster
raghu.8 is offline Offline
40 posts
since Nov 2007
Dec 1st, 2007
0

Re: populate two dropdownlist

You need to specify what country to use when you pull the data for the second drop down.

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Dec 3rd, 2007
0

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
Reputation Points: 10
Solved Threads: 0
Light Poster
raghu.8 is offline Offline
40 posts
since Nov 2007
Dec 3rd, 2007
0

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.

C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Dec 4th, 2007
0

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
Reputation Points: 10
Solved Threads: 0
Light Poster
raghu.8 is offline Offline
40 posts
since Nov 2007
Dec 4th, 2007
0

Re: populate two dropdownlist

ok, I will write the code example, and post it here in the next few minutes.
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Dec 4th, 2007
0

Re: populate two dropdownlist

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, 478 views)
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C# Forum Timeline: Highlight the data in the DataGrid when it is searched...
Next Thread in C# Forum Timeline: need help for distributed Application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC