Master/Detail DropDownList and DataGrid

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

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 Quick reply to this message  
Join Date: Sep 2007
Posts: 74
Reputation: reach_yousuf is an unknown quantity at this point 
Solved Threads: 13
reach_yousuf reach_yousuf is offline Offline
Junior Poster in Training

Re: Master/Detail DropDownList and DataGrid

 
0
  #2
Dec 2nd, 2008
hii
Add the 3rd line code while binding DDlist.
  1. ddlItems.DataSource = dsCategory;
  2. ddlItems.DataMember = "Categories";
  3. ddlItems.DataValueField= "CategoryID"
  4. ddlItems.DataTextField = "Description";
  5. ddlItems.DataS= "CategoryID";

Mark as sovled if it helps you!!!
Last edited by reach_yousuf; Dec 2nd, 2008 at 8:04 am.
Yousuf
Software Developer
Reply With Quote Quick reply to this message  
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

Re: Master/Detail DropDownList and DataGrid

 
0
  #3
Dec 2nd, 2008
I have all my categories loaded on my Dropdownlist but the problem is when I select a category the details of that category do not show on datagrid and I think the problem is on selectedIndexChanged event of the dropdownlist coz I am suppose to be selecting a category then the details appear on the datagrid but thats not working
Last edited by Traicey; Dec 2nd, 2008 at 8:09 am.
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 74
Reputation: reach_yousuf is an unknown quantity at this point 
Solved Threads: 13
reach_yousuf reach_yousuf is offline Offline
Junior Poster in Training

Re: Master/Detail DropDownList and DataGrid

 
0
  #4
Dec 2nd, 2008
As i understood from you code in selectindexchanged event:

  1. daItems.SelectCommand.CommandText = "Select ProductName, UnitPrice, UnitsInStock, ProductImage from dbo.Products where CategoryID = " + ddlItems.SelectedItem.Value;

You are passing param "categoryId", therefore u need to bind
dddl1.DataValueField = "categoryid"

Pls. let me know your exact concern
Yousuf
Software Developer
Reply With Quote Quick reply to this message  
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

Re: Master/Detail DropDownList and DataGrid

 
0
  #5
Dec 2nd, 2008
Yes I added that on my code but still it doesnt work or show the details that falls under that category in the datagrid
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 34
Reputation: vizy is an unknown quantity at this point 
Solved Threads: 6
vizy's Avatar
vizy vizy is offline Offline
Light Poster

Re: Master/Detail DropDownList and DataGrid

 
0
  #6
Dec 3rd, 2008
Try doing this.. it should work

Replace this code :
  1. ddlItems.DataSource = dsCategory;
  2. ddlItems.DataMember = "Categories";
  3. ddlItems.DataTextField = "Description";

With
  1. ddlItems.DataSource = dsCategory;
  2. ddlItems.DataValuefield = "CategoryId";
  3. ddlItems.DataTextField = "CategoryName";

and also puut all your code under Page_Load like this

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.  
  4. if(!IsPostback)
  5. {
  6. //This code is filling in the Dropdownlist with data from Category table
  7. // your complete Dropdown code here
  8.  
  9. }
  10. }

and in your SelectedIndexChanged function
change this

  1. DataSet dsItems = new DataSet(); daItems.SelectCommand.CommandText = "Select ProductName, UnitPrice, UnitsInStock, ProductImage from dbo.Products where CategoryID = " + [B]ddlItems.SelectedValue[/B];
Last edited by peter_budo; Dec 3rd, 2008 at 7:00 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Njoy koding... >>>
Reply With Quote Quick reply to this message  
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

Re: Master/Detail DropDownList and DataGrid

 
0
  #7
Dec 3rd, 2008
Thanx a lot man.... ur a star
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: divyasrinivasan is an unknown quantity at this point 
Solved Threads: 0
divyasrinivasan divyasrinivasan is offline Offline
Newbie Poster

Re: Master/Detail DropDownList and DataGrid

 
0
  #8
Dec 10th, 2008
hi above....
can u post ur entired code after making the necessry corrections pls....i need it urgently
Reply With Quote Quick reply to this message  
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

Re: Master/Detail DropDownList and DataGrid

 
0
  #9
Dec 10th, 2008
My code is partial working cozI still have a problem coz when I select add to cart on my 1st datagrid it overwrite the existing record instead of adding to it but everything else is working

But then again u need to understand how the code work hey if u dont let me know then I will explain
  1. /*Filling up the dropdownlist with each category. I used a procedure which select everything from Category which only have CategoryID and categoryDescription*/
  2. protected void Page_Load(object sender, EventArgs e)
  3. {
  4. if (!Page.IsPostBack)
  5. {
  6. string strCategory = "sdp_ViewCategory";
  7. DataSet dsCategory = new DataSet();
  8. SqlDataAdapter daCategory = new SqlDataAdapter(strCategory, conn);
  9. daCategory.Fill(dsCategory, "Categories");
  10. ddlItems.DataSource = dsCategory;
  11. ddlItems.DataValueField = "CategoryID";
  12. ddlItems.DataTextField = "Description";
  13. ddlItems.DataBind();
  14. ddlItems.Items.Insert(0, "Please select a category");
  15.  
  16. }
  17. }
  18.  
  19. /*Selecting a category from dropdownlist(DDL) and desplay the details of all the available product on that category depending on what I have selected on the DDL*/
  20. protected void ddlItems_SelectedIndexChanged(object sender, EventArgs e)
  21. {
  22.  
  23. dgItems.Visible = true;
  24. string strItems = "sdp_ViewCartAndProduct";
  25.  
  26. SqlDataAdapter daItems = new SqlDataAdapter(strItems, conn);
  27. if (ddlItems.SelectedIndex > 0)
  28. {
  29. DataSet dsItems = new DataSet();
  30. daItems.SelectCommand.CommandText = "Select ProductID, ProductName, PictureName, UnitsInStock from dbo.Products where CategoryID = " +
  31. ddlItems.SelectedValue;
  32.  
  33.  
  34. try
  35. {
  36. dsItems.Tables.Remove("Products");
  37. }
  38. catch (Exception de)
  39. {
  40. lblMsg.Text = de.Message;
  41. }
  42. daItems.Fill(dsItems, "Products");
  43. dgItems.DataSource = dsItems;
  44. dgItems.DataMember = "Products";
  45. dgItems.DataBind();
  46. dgItems.Visible = true;
  47. //dgItems.SelectedItem.Cells[4].Visible = false;
  48. foreach(DataGridColumn col in dgItems.Columns)
  49. {
  50. if (col.HeaderText == "PictureName")
  51. col.Visible = false;
  52.  
  53. }
  54. }
  55. else
  56. dgItems.Visible = false;
  57. }
  58.  
  59. /*Im clicking button select which I added on my 1st datagrid and named it to add to cart then I add what the user select on the 1st datagrid using the select button*/
  60. protected void dgItems_SelectedIndexChanged(object sender, EventArgs e)
  61. {
  62.  
  63. imgItems.Visible = true;
  64. imgItems.ImageUrl = "Pictures/" + dgItems.SelectedItem.Cells[4].Text;
  65. string strDetails = "sdp_ViewProducts";
  66. DataSet dsDetails = new DataSet();
  67. SqlDataAdapter daDetails = new SqlDataAdapter(strDetails, conn);
  68. daDetails.SelectCommand.CommandText = "Select ProductID, ProductName, UnitPrice from dbo.Products where ProductID = " +
  69. dgItems.SelectedItem.Cells[2].Text;
  70.  
  71. daDetails.Fill(dsDetails, "Products");
  72. dgDetails.DataSource = dsDetails;
  73. dgDetails.DataMember = "Products";
  74. dgDetails.DataKeyField = "ProductID";
  75. dgDetails.DataBind();
  76. dgDetails.Visible = true;
  77. }
Last edited by Traicey; Dec 10th, 2008 at 3:15 am.
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: divyasrinivasan is an unknown quantity at this point 
Solved Threads: 0
divyasrinivasan divyasrinivasan is offline Offline
Newbie Poster

Re: Master/Detail DropDownList and DataGrid

 
0
  #10
Dec 10th, 2008
pls give me the complete code for it ..i tryed but not getting the output..pls help..iam pasting the code here but no output


  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.  
  4.  
  5. Response.Write("sql is connected");
  6. SqlDataAdapter da = new SqlDataAdapter("select countryid,countryname from countryname", cn);
  7. DataSet ds = new DataSet();
  8. da.Fill(ds,"countryname");
  9. ddlcountryname.DataSource = ds.Tables["countryname"];
  10. ddlcountryname.DataTextField = "countryname";
  11. ddlcountryname.DataValueField = "countryid";
  12. ddlcountryname.DataBind();
  13.  
  14.  
  15. }
  16. protected void ddlcountryname_SelectedIndexChanged(object sender, EventArgs e)
  17. {
  18. string country_id;
  19. country_id = ddlcountryname.SelectedValue.ToString();
  20. SqlDataAdapter da1=new SqlDataAdapter("select stateid,statename from statename where countryid=' country_id'",cn);
  21. DataSet ds1 = new DataSet();
  22. da1.Fill(ds1, "statename");
  23. ddlstatename.DataSource = ds1.Tables["statename"];
  24. ddlstatename.DataTextField = "statename";
  25. ddlstatename.DataValueField = "stateid";
  26. ddlstatename.DataBind();
  27. }
my database is something like this...i have two tables countryname(countryid identity(1,1),countryname) and statename(stateid identity(100,1),statename,countryid)

so i have inserted 78 countries in country table in tat 73rd is india...
n i have inserted states of india in statename table(28)..how to get india's states wen india in selected
Last edited by peter_budo; Dec 10th, 2008 at 6:49 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC