943,578 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 3087
  • ASP.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 2nd, 2008
0

Master/Detail DropDownList and DataGrid

Expand Post »
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

C# Syntax (Toggle Plain Text)
  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. }

C# Syntax (Toggle Plain Text)
  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
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
Dec 2nd, 2008
0

Re: Master/Detail DropDownList and DataGrid

hii
Add the 3rd line code while binding DDlist.
ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 12
Solved Threads: 38
Junior Poster
reach_yousuf is offline Offline
194 posts
since Sep 2007
Dec 2nd, 2008
0

Re: Master/Detail DropDownList and DataGrid

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.
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
Dec 2nd, 2008
0

Re: Master/Detail DropDownList and DataGrid

As i understood from you code in selectindexchanged event:

ASP.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 12
Solved Threads: 38
Junior Poster
reach_yousuf is offline Offline
194 posts
since Sep 2007
Dec 2nd, 2008
0

Re: Master/Detail DropDownList and DataGrid

Yes I added that on my code but still it doesnt work or show the details that falls under that category in the datagrid
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
Dec 3rd, 2008
0

Re: Master/Detail DropDownList and DataGrid

Try doing this.. it should work

Replace this code :
asp.net Syntax (Toggle Plain Text)
  1. ddlItems.DataSource = dsCategory;
  2. ddlItems.DataMember = "Categories";
  3. ddlItems.DataTextField = "Description";

With
asp.net Syntax (Toggle Plain Text)
  1. ddlItems.DataSource = dsCategory;
  2. ddlItems.DataValuefield = "CategoryId";
  3. ddlItems.DataTextField = "CategoryName";

and also puut all your code under Page_Load like this

asp.net Syntax (Toggle Plain Text)
  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

asp.net Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 6
Light Poster
vizy is offline Offline
36 posts
since Dec 2007
Dec 3rd, 2008
0

Re: Master/Detail DropDownList and DataGrid

Thanx a lot man.... ur a star
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
Dec 10th, 2008
0

Re: Master/Detail DropDownList and DataGrid

hi above....
can u post ur entired code after making the necessry corrections pls....i need it urgently
Reputation Points: 7
Solved Threads: 0
Newbie Poster
divyasrinivasan is offline Offline
21 posts
since Dec 2008
Dec 10th, 2008
0

Re: Master/Detail DropDownList and DataGrid

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
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
Dec 10th, 2008
0

Re: Master/Detail DropDownList and DataGrid

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


asp.net Syntax (Toggle Plain Text)
  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.
Reputation Points: 7
Solved Threads: 0
Newbie Poster
divyasrinivasan is offline Offline
21 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: How to Send email using asp.net with C#
Next Thread in ASP.NET Forum Timeline: how to print the contents of text box





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC