954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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

SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["con"].ToString());
   
    protected void Page_Load(object sender, EventArgs e)
    {
       
        
        Response.Write("sql is connected");
        SqlDataAdapter da = new SqlDataAdapter("select countryid,countryname from countryname", cn);
        DataSet ds = new DataSet();
        da.Fill(ds,"countryname");
        ddlcountryname.DataSource = ds.Tables["countryname"];
        ddlcountryname.DataTextField = "countryname";
        ddlcountryname.DataValueField = "countryid";
        ddlcountryname.DataBind();
        

    }
    protected void ddlcountryname_SelectedIndexChanged(object sender, EventArgs e)
    {
        string country_id;
        country_id = ddlcountryname.SelectedValue.ToString();
        SqlDataAdapter da1=new SqlDataAdapter("select stateid,statename from statename where countryid=' country_id'",cn);
        DataSet ds1 = new DataSet();
        da1.Fill(ds1, "statename");
        ddlstatename.DataSource = ds1.Tables["statename"];
        ddlstatename.DataTextField = "statename";
        ddlstatename.DataValueField = "stateid";
        ddlstatename.DataBind();
    }

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

divyasrinivasan
Newbie Poster
21 posts since Dec 2008
Reputation Points: 7
Solved Threads: 0
 

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
}
}

vizy
Light Poster
36 posts since Dec 2007
Reputation Points: 10
Solved Threads: 6
 

hi,
did u set autopsotback to true for u r country dropdown .

greeny_1984
Posting Whiz
372 posts since Apr 2007
Reputation Points: 25
Solved Threads: 29
 

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

divyasrinivasan
Newbie Poster
21 posts since Dec 2008
Reputation Points: 7
Solved Threads: 0
 

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)+"

greeny_1984
Posting Whiz
372 posts since Apr 2007
Reputation Points: 25
Solved Threads: 29
 

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

divyasrinivasan
Newbie Poster
21 posts since Dec 2008
Reputation Points: 7
Solved Threads: 0
 

country table...
countryid countryname
1 america
2 brazil
3 india

divyasrinivasan
Newbie Poster
21 posts since Dec 2008
Reputation Points: 7
Solved Threads: 0
 

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);

Traicey
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 26
Solved Threads: 19
 

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())

greeny_1984
Posting Whiz
372 posts since Apr 2007
Reputation Points: 25
Solved Threads: 29
 

Hi

did you tried doing this.. ?

protected void Page_Load(object sender, EventArgs e)
{
     if(!IsPostback)
     {
            // your complete Country code here
     }
}


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

vizy
Light Poster
36 posts since Dec 2007
Reputation Points: 10
Solved Threads: 6
 

thanks a lot...vizy,greeny n traicey..u made it ..thanks a ton..n thanks for replying for my so called questions

divyasrinivasan
Newbie Poster
21 posts since Dec 2008
Reputation Points: 7
Solved Threads: 0
 

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

SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["con"].ToString());
   
    protected void Page_Load(object sender, EventArgs e)
    {
       
        
        Response.Write("sql is connected");
        SqlDataAdapter da = new SqlDataAdapter("select countryid,countryname from countryname", cn);
        DataSet ds = new DataSet();
        da.Fill(ds,"countryname");
        ddlcountryname.DataSource = ds.Tables["countryname"];
        ddlcountryname.DataTextField = "countryname";
        ddlcountryname.DataValueField = "countryid";
        ddlcountryname.DataBind();
        

    }
    protected void ddlcountryname_SelectedIndexChanged(object sender, EventArgs e)
    {
        string country_id;
        country_id = ddlcountryname.SelectedValue.ToString();
        SqlDataAdapter da1=new SqlDataAdapter("select stateid,statename from statename where countryid=' country_id'",cn);
        DataSet ds1 = new DataSet();
        da1.Fill(ds1, "statename");
        ddlstatename.DataSource = ds1.Tables["statename"];
        ddlstatename.DataTextField = "statename";
        ddlstatename.DataValueField = "stateid";
        ddlstatename.DataBind();
    }

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

I did a similar one But now sure whether this helps you.

I created a table like


tblState has:
1)State_Id(int 4,Identity Increment by 1)
2)CountryName(char 50 Allow Nulls)

3)State_Name(char 50 Allow Nulls)

The code I has is in classic asp.I am posting just to give an idea for you.Hope this helps you.

Set Conn = Server.CreateObject("ADODB.Connection")
connStr = "Provider=; Driver={SQL Server}; Server=sample; Database=data; DSN=login; UID=vari; PWD=ullet;"
Conn.Open(connStr)

sql = ""
sql = "select * from tblState"
Set RS = Conn.Execute(sql)
If RS.BOF And RS.EOF Then
    Error_Msg = "Database Connection Failed. Please contact the systems administrator."
Else
	do until RS.EOF
		region = RS.Fields("CountryName")
		commid = cdbl(RS.Fields("State_Id"))
		comm = RS.Fields("State_Name")
		
		if region = region_form then
			comm_options = comm_options & "<option value=" & commid & " selected>" & trim(comm) & "</option>"
		else
			comm_options = comm_options & "<option value=" & commid & ">" & trim(comm) & "</option>"
		end if	
		RS.movenext
	loop
End If


If you need more briefly let me know i can help you

terry

terrible
Newbie Poster
2 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You