943,744 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 2371
  • ASP.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 9th, 2008
0

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

Expand Post »
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
aspnet Syntax (Toggle Plain Text)
  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
Reputation Points: 7
Solved Threads: 0
Newbie Poster
divyasrinivasan is offline Offline
21 posts
since Dec 2008
Dec 9th, 2008
0

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

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
}
}
Reputation Points: 10
Solved Threads: 6
Light Poster
vizy is offline Offline
36 posts
since Dec 2007
Dec 10th, 2008
0

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

hi,
did u set autopsotback to true for u r country dropdown .
Reputation Points: 25
Solved Threads: 29
Posting Whiz
greeny_1984 is offline Offline
372 posts
since Apr 2007
Dec 10th, 2008
0

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

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
Reputation Points: 7
Solved Threads: 0
Newbie Poster
divyasrinivasan is offline Offline
21 posts
since Dec 2008
Dec 10th, 2008
0

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

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)+"
Reputation Points: 25
Solved Threads: 29
Posting Whiz
greeny_1984 is offline Offline
372 posts
since Apr 2007
Dec 10th, 2008
0

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

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
Reputation Points: 7
Solved Threads: 0
Newbie Poster
divyasrinivasan is offline Offline
21 posts
since Dec 2008
Dec 10th, 2008
0

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

country table...
countryid countryname
1 america
2 brazil
3 india
Reputation Points: 7
Solved Threads: 0
Newbie Poster
divyasrinivasan is offline Offline
21 posts
since Dec 2008
Dec 10th, 2008
0

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

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);
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
Dec 10th, 2008
0

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

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())
Reputation Points: 25
Solved Threads: 29
Posting Whiz
greeny_1984 is offline Offline
372 posts
since Apr 2007
Dec 10th, 2008
0

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

Hi

did you tried doing this.. ?
asp.net Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 6
Light Poster
vizy is offline Offline
36 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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.
Message:
Previous Thread in ASP.NET Forum Timeline: Mac Address in ASP.net using vb.net / Ajax
Next Thread in ASP.NET Forum Timeline: Hiding Columns in the datagrid





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


Follow us on Twitter


© 2011 DaniWeb® LLC