Hi Everyone,
I am facing a small problem with drop down list.I am having two drop down list.One is having Countries names.We
should have select one of those countries ,then the second drop down list displays states in order to select of
the country name from the first drop down list.
Thanks,
M.Maheswar
Loading
Satyapriya NayakPosted Oct 18, 2011, 11:13 AM
Here is your solution
Run the attachments.
create table area(state varchar(50),country varchar(50))
Default.aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
Default.aspx.cs code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
if (!IsPostBack)
{
DropDownList_country.Items.Add("Choose country");
con.Open();
str = "select country from area group by country";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DropDownList_country.Items.Add(reader["country"].ToString());
}
reader.Close();
con.Close();
}
DropDownList_state.Items.Clear();
}
protected void DropDownList_country_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from area where country='" + DropDownList_country.SelectedItem.Text + "'";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DropDownList_state.Items.Add(reader["state"].ToString());
}
reader.Close();
con.Close();
}
}
Thanks
If this post helps you mark it as answer
Dhaval PatelPosted Oct 18, 2011, 8:20 AM
protected void Page_Load(object sender, System.EventArgs e)
{
Getcountrydetails();
}
public void Getcountrydetails()
{
DataSet ds = new DataSet();
ds = Returnresult(countryname);
state.DataSource = ds.Tables[0];
state.DataTextField = ds.Tables[0].Columns["statename"].ColumnName.ToString();
state.DataValueField = ds.Tables[0].Columns["state_Id"].ColumnName.ToString();
state.DataBind();
}
public void getStatedetails()
{
DataSet ds = new DataSet();
ds = Returnresult1(countryname);
product.DataSource = ds.Tables[0];
product.DataTextField = ds.Tables[0].Columns["productname"].ColumnName.ToString();
product.DataValueField = ds.Tables[0].Columns["productcode"].ColumnName.ToString();
product.DataBind();
}
public DataSet Returnresult(countryname)
{
string query = null;
query = "select country_name,country_id from country;
DataSet ds = null;
ds = new DataSet();
ds = returndataset(query, CommandType.Text);
return ds;
}
public DataSet Returnresult1(countryname)
{
string query = null;
query = "select statename,state_Id from country where country_name ='"+ countryname + "';
DataSet ds = null;
ds = new DataSet();
ds = returndataset(query, CommandType.Text);
return ds;
}
protected void country_SelectedIndexChanged(object sender, System.EventArgs e)
{
stateDetails(countr.selecteditem.text);
}
}
Mamta MPosted Oct 4, 2011, 3:19 AM
http://www.c-sharpcorner.com/UploadFile/rohatash/9251/
maheswar reddyPosted Oct 4, 2011, 3:11 AM
Here I am developing asp.net page but i am witting this code in .cs file in C Sharp.
Thanks,
M.Maheswar
Mamta MPosted Oct 4, 2011, 3:09 AM