Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
on page load the country dropdownlist is bind and working fine but after when i select the country than no state bind in state dropdownlist.

What I have tried:

C#
 ----------------------front end-----------------------------
<asp:DropDownList ID="ddlcountry" runat="server" OnSelectedIndexChanged="ddlcountry_SelectedIndexChanged" AutoPostBack="true" CssClass="form-control ddl"></asp:DropDownList>
 
<asp:DropDownList ID="ddlstate" runat="server" AutoPostBack="true" CssClass="form-control ddl"></asp:DropDownList> 
 
--------------------------------code behind------------------------------ 
 
protected void Page_Load(object sender, EventArgs e)
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
if (!Page.IsPostBack)
{
getcountry();
}
}
protected void getcountry()
{
try
{
da = new SqlDataAdapter("select * from get_country", con);
ds = new DataSet();
da.Fill(ds);
ddlcountry.DataSource = ds;
ddlcountry.DataTextField = "conname";
ddlcountry.DataValueField = "conid";
ddlcountry.DataBind();
ddlcountry.Items.Insert(0, new ListItem("--Select--", "0"));
ddlstate.Items.Insert(0, new ListItem("--Select--", "0"));
}
catch (Exception ex)
{
Response.Write("error occurd :" + ex.Message.ToString());
}
finally
{
con.Close();
}
}
protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int conid = Convert.ToInt32(ddlcountry.SelectedItem.Value.ToString());
da = new SqlDataAdapter("select stateid,statename from get_state where conid=" + conid, con);
ds = new DataSet();
da.Fill(ds);
ddlstate.DataSource = ds;
ddlstate.DataTextField = "statename";
ddlstate.DataValueField = "stateid";
ddlstate.DataBind();
ddlstate.Items.Insert(0, new ListItem("---Select State---", "0"));
if (ddlstate.SelectedValue == "0")
{
ddlcity.Items.Clear();
ddlcity.Items.Insert(0,new ListItem("---Select City---","0"));
}
}
catch(Exception ex)
{
Response.Write("error occurd :" + ex.Message.ToString());
}
finally
{
con.Close();
}
}
Posted
Updated 19-Aug-16 20:03pm
Comments
Umair Nafis 19-Aug-16 14:50pm    
----------------Code Behind--------------------

protected void Page_Load(object sender, EventArgs e)
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
if (!Page.IsPostBack)
{
getcountry();
}
}
protected void getcountry()
{
try
{
da = new SqlDataAdapter("select * from get_country", con);
ds = new DataSet();
da.Fill(ds);
ddlcountry.DataSource = ds;
ddlcountry.DataTextField = "conname";
ddlcountry.DataValueField = "conid";
ddlcountry.DataBind();
ddlcountry.Items.Insert(0, new ListItem("--Select--", "0"));
ddlstate.Items.Insert(0, new ListItem("--Select--", "0"));
}
catch (Exception ex)
{
Response.Write("error occurd :" + ex.Message.ToString());
}
finally
{
con.Close();
}
}
protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int conid = Convert.ToInt32(ddlcountry.SelectedItem.Value.ToString());
da = new SqlDataAdapter("select stateid,statename from get_state where conid=" + conid, con);
ds = new DataSet();
da.Fill(ds);
ddlstate.DataSource = ds;
ddlstate.DataTextField = "statename";
ddlstate.DataValueField = "stateid";
ddlstate.DataBind();
ddlstate.Items.Insert(0, new ListItem("---Select State---", "0"));
if (ddlstate.SelectedValue == "0")
{
ddlcity.Items.Clear();
ddlcity.Items.Insert(0,new ListItem("---Select City---","0"));
}
}
catch(Exception ex)
{
Response.Write("error occurd :" + ex.Message.ToString());
}
finally
{
con.Close();
}
}

1 solution

based on the code from Comments

C#
ddlstate.Items.Insert(0, new ListItem("---Select State---", "0")); // line 1
if (ddlstate.SelectedValue == "0")  // line 2
{
ddlcity.Items.Clear();  // line 3
ddlcity.Items.Insert(0,new ListItem("---Select City---","0"));
}


in line 1 you are inserting a value at 0th index, which will be selected by default
in line 2 you are checking the selected value is equals 0, of course it will be 0 only, it will pass inside the condition
in line 3 it will clear all the items in ddlcity
ultimately your ddlstate will be empty,

Solution:
C#
if (ddlCountry.SelectedValue == "0") // change state to country
 
Share this answer
 
Comments
Umair Nafis 20-Aug-16 5:34am    
still not working
Karthik_Mahalingam 20-Aug-16 5:35am    
post the updated code here
Umair Nafis 20-Aug-16 6:51am    
i have deleted all the code and start from scratch....now i know the problem is in sql query which i write in as :-

da = new SqlDataAdapter("select stateid,statename from get_state where conid=" + conid, con);

do have any idea about this sytnax is correct or not ?
i even use stored procesdure in place of direct query bt the result is still same it is not taking any value from the database
Karthik_Mahalingam 20-Aug-16 7:01am    
yes correct
Umair Nafis 20-Aug-16 6:55am    
--------------this is my code after using stored procedure---------------
protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{

cmd = new SqlCommand("_pr_get_state", con);
cmd.CommandType=CommandType.StoredProcedure;
da=new SqlDataAdapter(cmd);
ds=new DataSet();
da.Fill(ds);
ddlstate.DataSource=ds;
ddlstate.DataTextField="statename";
ddlstate.DataValueField="stateid";
ddlstate.DataBind();
ddlstate.Items.Insert(0, new ListItem("---Select State---", "0"));
ddlcity.Items.Insert(0, new ListItem("---Select City---", "0"));
if (ddlstate.SelectedValue == "0")
{

ddlcity.Items.Clear();
ddlcity.Items.Insert(0, new ListItem("---Select City---", "0"));

}


}

---------and this query in sql server-------------

CREATE PROCEDURE _pr_get_state
@conid int
AS
BEGIN
select stateid,statename from get_state where conid=@conid
END

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900