View Single Post
Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

Master/Detail DropDownList and DataGrid

 
0
  #1
Dec 2nd, 2008
Guys

I have a dropdownlist control with categories so when I select something from ddlCategory I wana display all the products fall under that category in a datagrid but I dont seem to get that right... here is the bit I have done

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. //This code is filling in the Dropdownlist with data from Category table
  4. string strCategory = "sdp_ViewCategory";
  5. DataSet dsCategory = new DataSet();
  6. SqlDataAdapter daCategory = new SqlDataAdapter (strCategory, conn);
  7. daCategory.Fill(dsCategory, "Categories");
  8. ddlItems.DataSource = dsCategory;
  9. ddlItems.DataMember = "Categories";
  10. ddlItems.DataTextField = "Description";
  11. // ddlItems.DataSourceID = "CategoryID";
  12. ddlItems.DataBind();
  13. ddlItems.Items.Insert(0, "Please select a category");
  14. }

  1. protected void ddlItems_SelectedIndexChanged(object sender, EventArgs e)
  2. {
  3. //Select a category and display datagrid
  4. dgItems.Visible = true;
  5. string strItems = "sdp_ViewCartAndProduct";
  6. SqlDataAdapter daItems = new SqlDataAdapter(strItems, conn);
  7. if (ddlItems.SelectedIndex > 0)
  8. {
  9. DataSet dsItems = new DataSet();
  10. daItems.SelectCommand.CommandText = "Select ProductName, UnitPrice, UnitsInStock, ProductImage from dbo.Products where CategoryID = "
  11. + ddlItems.SelectedItem.Value;
  12. try
  13. {
  14. dsItems.Tables.Remove("Products");
  15. }
  16. catch (Exception de)
  17. {
  18. lblMsg.Text = de.Message;
  19. }
  20. daItems.Fill(dsItems, "Products");
  21. dgItems.DataSource = dsItems;
  22. dgItems.DataMember = "Products";
  23. dgItems.DataKeyField = "CategoryID";
  24. dgItems.DataBind();
  25. dgItems.Visible = true;
  26. }
  27. else
  28. dgItems.Visible = false;
  29. }

Thanx in advance
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote