How to get a state from database in combobox

Posted by Rock_Monu under ASP.NET on 8/6/2012 | Points: 10 | Views : 843 | Status : [Member] | Replies : 1
How to get statename from database in combobox according countryname by countryid..??




Responses

Posted by: Ranjeet_8 on: 8/6/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
string strCon = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

string str;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;

void bindCountry()
{
SqlConnection con = new SqlConnection(strCon);
str = "select CountryId,CountryName from Country order by CountryName asc";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds,"Country");
comCountry.DataSource = ds.Tables["Country"].DefaultView;
comCountry.ValueMember = "CountryId";
comCountry.DisplayMember = "CountryName";
}

// Combobox Selected Country Changed 'Start'

bindState();

// End
void bindState()
{

SqlConnection con = new SqlConnection(strCon);
str = "select StateId,StateName from State where Country_Id ='" + comCountry.SelectedValue + "'";
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";
}


Rock_Monu, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response