Hi,
Create one webApplication, and two classLibrary.
In Web Application:
Code In Aspx Page:
<form id="form1" runat="server">
<div>
<asp:dropdownlist id="Country" runat="server" xmlns:asp="#unknown">
onselectedindexchanged="CountryValueSelectedIndexChanged"
AutoPostBack="True">
</asp:dropdownlist>
<br />
<asp:dropdownlist id="State" runat="server" xmlns:asp="#unknown">
</asp:dropdownlist>
</div>
</form>
Code with in Aspx.cs Page:
BusinessLogic businessLogic = new BusinessLogic();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CountryBind();
}
}
public void CountryBind()
{
DataSet countryDataSet = businessLogic.CountryValues();
Country.DataSource = countryDataSet;
Country.DataTextField = "CountryName";
Country.DataValueField = "ID";
Country.DataBind();
Country.Items.Insert(0,new ListItem("-- Select --", "0"));
}
protected void CountryValueSelectedIndexChanged(object sender, EventArgs e)
{
int countryid = Convert.ToInt32(Country.SelectedValue);
DataSet countryDataSet = businessLogic.selectState(Country.SelectedIndex);
State.DataSource = countryDataSet;
State.DataTextField = "StateName";
State.DataValueField = "CountryCode";
State.DataBind();
}
Create one class Library named as BusinessLayer. With in that rename Program.cs to BusinessLogic.cs.
Code with in BusinessLogic.cs is:
public class BusinessLogic
{
DataLogic dbProgram = new DataLogic();
public DataSet selectState(int countryValue)
{
return dbProgram.returnState(countryValue);
}
public DataSet CountryValues()
{
return dbProgram.Countries();
}
}
Create one class Library named as DataLayer. With in that rename Program.cs to DataLogic.cs.
Code with in DataLogic.cs is:
public class DataLogic
{
SqlConnection con = new SqlConnection("Data Source=HYD-NPULIVARTHY;Initial Catalog=test2;Integrated Security=True");
public DataSet returnState(int countryId)
{
// SqlConnection con = new SqlConnection("Data Source=HYD-NPULIVARTHY;Initial Catalog=test2;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select * from State where CountryCode='" + countryId + "'", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
cmd.ExecuteNonQuery();
da.Fill(ds);
con.Close();
return ds;
}
public DataSet Countries()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from CountryState", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
cmd.ExecuteNonQuery();
da.Fill(ds);
con.Close();
return ds;
}
}
After creating creating 3 Layers, goto Web Application then goto References.
RightClick on the References-> select Project and select BusinessLayer Reference.
Goto BusinessLayer then goto References.
RightClick on the References-> select Project and select DataLayer Reference.
This is the way to implement.