Reputation: 55
I have two tables; they are tblcity
and tblarea
in database like:
tblcity
city id | city
--------------
1 | ktm
2 | bkt
3 | lat
tblcity
areaid | cityid | area
----------------------
1 | 1 | a
2 | 1 | b
3 | 1 | c
4 | 2 | d
5 | 2 | e
6 | 3 | f
7 | 3 | g
The city table consists of three city name i.e ktm, bkt, lat. Whereas table area consists the name of area in that city.
I have shown all the city name in one dropdownlist i.e DropDownList1. Now I am trying to do is to show all the respective area name of that city in another dropdownlist i.e in DropDownList7 when i select one of the city name in DropDownList1.
Like if select city ktm in DropDownList1 only area name like a, b, c of city ktm should be should be shown in DropDownList7.
Now the problem is that, here I am able to show all the city name in DropDownList1 but on selecting one of the city in DropDownList1 the area name belonging to that city are not shown in DropDownList7.
<asp:DropDownList ID="DropDownList1" CssClass="form-control shadow2 input-lg" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList7" runat="server" Height="111px" Width="376px"></asp:DropDownList>
Code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = c.getallcity();
if (dt.Rows.Count>0)
{
DataRow dr = dt.NewRow();
dr["city"] = "select city ";
dt.Rows.InsertAt(dr, 0);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "city";
DropDownList1.DataValueField = "cityid";
DropDownList1.DataBind();
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex!= 0)
{
DataTable dt = c.getareabycityid(Convert.ToInt32(DropDownList1.SelectedValue.ToString()));
if (dt.Rows.Count>0)
{
DataRow dr = dt.NewRow();
dr["area"] = "select area ";
dt.Rows.InsertAt(dr, 0);
DropDownList7.DataSource = dt;
DropDownList7.DataTextField = "area";
DropDownList7.DataValueField = "areaid";
DropDownList7.DataBind();
}
}
}
Method used
public DataTable getallcity()
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
string sql = "select * from tblcity";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
//---datasource---da----datatable,dataset
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable getareabycityid(int cityid)
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
string sql = "select * from tblarea where cityid=@cityid";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@cityid", cityid);
SqlDataAdapter da = new SqlDataAdapter(cmd);
//---datasource---da----datatable,dataset
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
Upvotes: 0
Views: 61
Reputation: 3326
First of all, your <asp:DropDownList ID="DropDownList1">
should contains Autopostback property like this :
<asp:DropDownList id="ddlId" runat="server" AutoPostBack="True"/>
then i would put a break point just before
if (dt.Rows.Count>0)
{
}
and check what this DataTable dt
contains
Upvotes: 1
Reputation: 77934
You need to set the property AutoPostBack = true
for your dropdown list DropDownList1
Upvotes: 0