Hi All,
I am very new to programing, in my sql database i have tables by the name of State and District and entries of StateId, StateName, DistrictId, DistrictName etc, what i want is that if state is selected in combo1 so according to the Stateid the district name should appear in combo2 and vice-versa, i m using visual studio 2010 with sql server, i have copied the following coding from the internet but didnt work, any guide will be very helpful thnx
string strCon = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
void bindstate()
{
SqlConnection con = new SqlConnection(strCon);
str = "select StateId,StateName from State order by StateName asc";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds,"State");
comstate.DataSource = ds.Tables["State"].DefaultView;
comstate.ValueMember = "StateId";
comstate.DisplayMember = "StateName";
}
// Combobox Selected State Changed 'Start'
bindDistrict();
// End
void bindDistrict()
{
SqlConnection con = new SqlConnection(strCon);
str = "select DistrictId,DistrictName from District where State_Id ='" + comstate.SelectedValue + "'";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds,"District");
comDistrict.DataSource = ds.Tables["District"].DefaultView;
comDistrict.ValueMember = "DistrictId";
comDistrict.DisplayMember = "DistrictName";
}