C# .NET - dropdown list

Asked By kiran Kumar
17-May-11 08:53 AM
hi,

 i have created table with fields category, product and product-price as follows:

category      product      product-price
----------      ----------     -----------------
c001              p1              10
                     p2              20
                     p3              30

c002              p4               15
                     p5               25

c003              p6                12
                     p7                22
..........

  in my asp webform i have placed 2 drop down list controls and one label control

drop down list 1  have to show categories c001,c002,c003..... loading automatically from database table , if i select category coo1 from drop down list1, then drop down list 2 have to show the products p1,p2,p3  and if i select p1 then label display p1 price

   similarly if i select coo2 from drop down list1, then drop down list2 will have to contain the items p4, p5 and the same ....how to do this

please explain me in detail and give me the exact solution for this , i have posted the same so many times but i hadn't 
got the exact answer, please give me the solution
  Vickey F replied to kiran Kumar
17-May-11 09:04 AM

If you want to display data based on DropDownList selection, means if you want to implement cascading then you have to implement code in  SelectedIndexChanged() Event of DropDownList.

I m giving sample code for that-

1. here i m filling DropDownList1 with category    

//for binding DropDownList1 with category    

private void DataGrid_Load(object sender, EventArgs e)
{
 getdata();
}

//function for getting category    
private void getdata()
{
SqlConnection cn = new SqlConnection("constring");
cn.Open();
string strQuery = "Select categoryid,categoryname from category ";
SqlDataAdapter da = new SqlDataAdapter(strQuery, cn);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DisplayMember = "categoryid";
DropDownList1.ValueMember = "categoryname ";
DropDownList1.DataSource = ds.Tables[0];
}

2. here i m filling DropDownList2 with product  , based on selection on DropDownList1

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("constring");
cn.Open();
string strQuery = "Select productid,productname from producttab where categoryid='" + DropDownList1.SelectedValue .ToString() + "'";
SqlDataAdapter da = new SqlDataAdapter(strQuery, cn);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList2 .DisplayMember = "productid";
DropDownList2.ValueMember = "productname ";
DropDownList2.DataSource = ds.Tables[0];
}

 

3. here i m filling DropDownList3 with product-price, based on selection on DropDownList2

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("constring");
cn.Open();
string strQuery = "Select product-price from  product-pricetab where categoryid='" + DropDownList1.SelectedValue .ToString() + "' and productid='" + DropDownList2.SelectedValue .ToString() + "' ";
SqlDataAdapter da = new SqlDataAdapter(strQuery, cn);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList3.DisplayMember = "product-price";
DropDownList3.ValueMember = "product-price";
DropDownList3.DataSource = ds.Tables[0];
}

USE THIS CODE AND LET ME KNOW.

 

Create New Account
help
dropdownlist n gridview i ws tryng to retrive the data frm sql database nn have to in gridview protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { string connStr = ConfigurationManager.ConnectionStrings["MyConn77"].ToString(); SqlConnection conn = new SqlConnection(connStr); SqlDataAdapter ad = new SqlDataAdapter("select name from [form] where autoid = @autoid" , conn); DataTable dt = new DataTable (); ad.Fill(dt the code protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { string connStr = ConfigurationManager.ConnectionStrings["MyConn77"].ToString(); SqlConnection conn = new SqlConnection(connStr); SqlDataAdapter ad = new SqlDataAdapter("select * from [form] where autoid = @autoid" , conn); DataTable dt = new DataTable (); ad
DROPDOWNLIST IN C# HI FRIENDS, I HAVE A TWO DROPDOWNLIST. FIRST DROPDOWNLIST FOR STATES, SECOND DROPDOWNLIST FOR CAPITAL. I WANT TO SHOW LIKE THIS. IF SELECTED STATE DROPDOWNLIST , AUTOMATICALLY SHOWS THE REVELANT CAPITAL IN SECOND DROPDOWNLIST WITH USING DATABASE. First of all set the AutoPostBack = True for all the dropdownlist. then implement the selectedIndexChanged This way protected void DropDownList1_SelectedIndexChanged( object sender, EventArgs e) { string s = DropDownList1.SelectedValue.ToString(); SqlConnection conn = new SqlConnection (); conn.ConnectionString = ConfigurationManager .ConnectionStrings[ "ConnectionString" ].ConnectionString; SqlDataAdapter da = new SqlDataAdapter ( "select State
dropdownlist i ws tring to bring a name list from my table !! protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { string connStr = ConfigurationManager.ConnectionStrings["MyConn77"].ToString(); SqlConnection conn = new SqlConnection(connStr); SqlDataAdapter ad = new SqlDataAdapter("select name from [submit]", conn); DataSet dSet = new DataSet(); ad.Fill(dSet, "PagesData"); DropDownList1.DataSource datavalue field protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { string connStr = ConfigurationManager.ConnectionStrings["MyConn77"].ToString(); SqlConnection conn = new SqlConnection(connStr); SqlDataAdapter ad = new SqlDataAdapter("select name from [submit]", conn); datatable dt = new datatable (); ad.Fill(dt
dropdownlist i m using one dropdownlist in aspx and i hv created a table in sql server now i tryng to i hv givn connection . . .ERROR occuring !! private void BindData() { string connStr = ConfigurationManager.ConnectionStrings["MyDbConn1"].ConnectionString; SqlConnection conn = new SqlConnection(connStr); SqlDataAdapter ad = new SqlDataAdapter("select * from [state]", conn); DataTable dt = new DataTable(); ad.Fill(dt); DropDownList.DataSource = dt; DropDownList.DataBind(); } protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e) { } Error 1 A using namespace directive can
column rcord into dropdownlist hai friends, i have table in sqlserver .in the table it contains column named users. how can i get the column values in to a dropdownlist void bindDropDonw() { SqlDataAdapter da = new SqlDataAdapter ( "select users from emp" , "ConnectionString" ); DataTable dt = new DataTable (); da.Fill(dt); / / Fill the dataset DropDownList1.DataBind(); } thanks dipa it works Your Welcome! protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { DropDownList dropDownList = (DropDownList)sender; GridViewRow parentRow = (GridViewRow)dropDownList.NamingContainer; string startTime = parentRow.Cells(0).Text; } Use this code- protected void GetDetails_Click(object sender