Binding the data to combobox from sql

Posted by Sumasri under C# on 8/2/2012 | Points: 10 | Views : 5140 | Status : [Member] | Replies : 3
Hi
what is the method to bind the data to combobox from database. and I have taken to combos like state and district, and I have taken stateid in both the tables in database. Now how to implent the code when i select the state then the related district names have to be displayed.

Please suggest me the code.

Thanks

sumasri


Responses

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

Up
0
Down
    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";

}


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

Posted by: Sathiya_Narayanan on: 8/3/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
Refer this link.
http://shawpnendu.blogspot.in/2009/05/how-to-bind-or-populate-data-into.html

SATHIYANARAYANAN

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

Posted by: Sathiya_Narayanan on: 8/3/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
You can do this in AJAX also
www.aspdotnet-suresh.com/2010/10/how-to-populate-dropdown-based-on-other.html

SATHIYANARAYANAN

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

Login to post response