on selecting country from one dropdownlist ...states of selected country is not getti

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 21
Reputation: divyasrinivasan is an unknown quantity at this point 
Solved Threads: 0
divyasrinivasan divyasrinivasan is offline Offline
Newbie Poster

on selecting country from one dropdownlist ...states of selected country is not getti

 
0
  #1
Dec 9th, 2008
pls give me the complete code for it ..i tryed but not getting the output..pls help..iam pasting the code here but no output
  1. SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["con"].ToString());
  2.  
  3. protected void Page_Load(object sender, EventArgs e)
  4. {
  5.  
  6.  
  7. Response.Write("sql is connected");
  8. SqlDataAdapter da = new SqlDataAdapter("select countryid,countryname from countryname", cn);
  9. DataSet ds = new DataSet();
  10. da.Fill(ds,"countryname");
  11. ddlcountryname.DataSource = ds.Tables["countryname"];
  12. ddlcountryname.DataTextField = "countryname";
  13. ddlcountryname.DataValueField = "countryid";
  14. ddlcountryname.DataBind();
  15.  
  16.  
  17. }
  18. protected void ddlcountryname_SelectedIndexChanged(object sender, EventArgs e)
  19. {
  20. string country_id;
  21. country_id = ddlcountryname.SelectedValue.ToString();
  22. SqlDataAdapter da1=new SqlDataAdapter("select stateid,statename from statename where countryid=' country_id'",cn);
  23. DataSet ds1 = new DataSet();
  24. da1.Fill(ds1, "statename");
  25. ddlstatename.DataSource = ds1.Tables["statename"];
  26. ddlstatename.DataTextField = "statename";
  27. ddlstatename.DataValueField = "stateid";
  28. ddlstatename.DataBind();
  29. }
my database is something like this...i have two tables countryname(countryid identity(1,1),countryname) and statename(stateid identity(100,1),statename,countryid)

so i have inserted 78 countries in country table in tat 73rd is india...
n i have inserted states of india in statename table(28)..how to get india's states wen india in selected
Last edited by cscgal; Dec 9th, 2008 at 1:16 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 34
Reputation: vizy is an unknown quantity at this point 
Solved Threads: 6
vizy's Avatar
vizy vizy is offline Offline
Light Poster

Re: on selecting country from one dropdownlist ...states of selected country is not getti

 
0
  #2
Dec 9th, 2008
Hi..

Put all the code in Page load event in !IspostBack like below..

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostback)
{
// your code here
}
}
Njoy koding... >>>
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 370
Reputation: greeny_1984 is an unknown quantity at this point 
Solved Threads: 29
greeny_1984's Avatar
greeny_1984 greeny_1984 is offline Offline
Posting Whiz

Re: on selecting country from one dropdownlist ...states of selected country is not getti

 
0
  #3
Dec 10th, 2008
hi,
did u set autopsotback to true for u r country dropdown .
If u r query is achieved,mark the thread as solved

Live and Let Live
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: divyasrinivasan is an unknown quantity at this point 
Solved Threads: 0
divyasrinivasan divyasrinivasan is offline Offline
Newbie Poster

Re: on selecting country from one dropdownlist ...states of selected country is not getti

 
0
  #4
Dec 10th, 2008
if i keep autopostback to tru ...if i select some country say america...iam getting the first country selected ..tat is afganisthan....
i have
afganisthan
america
india
say if i select india afganisthan is only coming.....n i want databasee like how to insert states for country india....like i said i have 3 fields...but i have 28 states....how to write table...
lot of confusion
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 370
Reputation: greeny_1984 is an unknown quantity at this point 
Solved Threads: 29
greeny_1984's Avatar
greeny_1984 greeny_1984 is offline Offline
Posting Whiz

Re: on selecting country from one dropdownlist ...states of selected country is not getti

 
0
  #5
Dec 10th, 2008
hi,
inoder to get states its compulsory that u set autopostback to true.The Reply u gave is confusing to me.
can u be eloborate .

coming to creating table

table1

countryid

country name


table 2

id

country_id

state_id

state_name

when u select a country

the query for u r state would be

select state_id,state_name from table2 where country_id="+int.parse(ddlcountry.selectedvalue)+"
If u r query is achieved,mark the thread as solved

Live and Let Live
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: divyasrinivasan is an unknown quantity at this point 
Solved Threads: 0
divyasrinivasan divyasrinivasan is offline Offline
Newbie Poster

Re: on selecting country from one dropdownlist ...states of selected country is not getti

 
0
  #6
Dec 10th, 2008
the query u gave me i wrote it ..u can see tat...but there is no output...
like
stateis statename countryid
100 Ahmedabad 1
101 guntur 2
103 -----

see these r alll same for india ..wat i shld do but country id 1 ,2 ,3
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: divyasrinivasan is an unknown quantity at this point 
Solved Threads: 0
divyasrinivasan divyasrinivasan is offline Offline
Newbie Poster

Re: on selecting country from one dropdownlist ...states of selected country is not getti

 
0
  #7
Dec 10th, 2008
country table...
countryid countryname
1 america
2 brazil
3 india
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

Re: on selecting country from one dropdownlist ...states of selected country is not getti

 
0
  #8
Dec 10th, 2008
SqlDataAdapter da1=new SqlDataAdapter("select stateid,statename from statename where countryid=' country_id'",cn);
Try and fix this line cos you dont need to put the country_id within single and Double qoutations marks which u have declared and set its value to the one selected on DDL

The better sollution will be
SqlDataAdapter da1=new SqlDataAdapter("select stateid,statename from statename where countryid = " + country_id ,cn);
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 370
Reputation: greeny_1984 is an unknown quantity at this point 
Solved Threads: 29
greeny_1984's Avatar
greeny_1984 greeny_1984 is offline Offline
Posting Whiz

Re: on selecting country from one dropdownlist ...states of selected country is not getti

 
0
  #9
Dec 10th, 2008
did u declare int for countryid ,if so u r storing string in int column

try to convert string to int
using

int.parse(ddlcountry.selectedvalue.tostring())
If u r query is achieved,mark the thread as solved

Live and Let Live
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 34
Reputation: vizy is an unknown quantity at this point 
Solved Threads: 6
vizy's Avatar
vizy vizy is offline Offline
Light Poster

Re: on selecting country from one dropdownlist ...states of selected country is not getti

 
0
  #10
Dec 10th, 2008
Hi

did you tried doing this.. ?
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if(!IsPostback)
  4. {
  5. // your complete Country code here
  6. }
  7. }

I think this is the only thing u are missing...
bocz after the country is selected the page gets PostBack to server
and execute the Page_Load everytime.. that's the reason u are getting "Afghanistan" selected even after selecting "America" as it is getting bind everytime
Last edited by peter_budo; Dec 12th, 2008 at 6:56 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Njoy koding... >>>
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC