| | |
Master/Detail DropDownList and DataGrid
Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved |
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
Thanx in advance
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)
protected void Page_Load(object sender, EventArgs e) { //This code is filling in the Dropdownlist with data from Category table string strCategory = "sdp_ViewCategory"; DataSet dsCategory = new DataSet(); SqlDataAdapter daCategory = new SqlDataAdapter (strCategory, conn); daCategory.Fill(dsCategory, "Categories"); ddlItems.DataSource = dsCategory; ddlItems.DataMember = "Categories"; ddlItems.DataTextField = "Description"; // ddlItems.DataSourceID = "CategoryID"; ddlItems.DataBind(); ddlItems.Items.Insert(0, "Please select a category"); }
C# Syntax (Toggle Plain Text)
protected void ddlItems_SelectedIndexChanged(object sender, EventArgs e) { //Select a category and display datagrid dgItems.Visible = true; string strItems = "sdp_ViewCartAndProduct"; SqlDataAdapter daItems = new SqlDataAdapter(strItems, conn); if (ddlItems.SelectedIndex > 0) { DataSet dsItems = new DataSet(); daItems.SelectCommand.CommandText = "Select ProductName, UnitPrice, UnitsInStock, ProductImage from dbo.Products where CategoryID = " + ddlItems.SelectedItem.Value; try { dsItems.Tables.Remove("Products"); } catch (Exception de) { lblMsg.Text = de.Message; } daItems.Fill(dsItems, "Products"); dgItems.DataSource = dsItems; dgItems.DataMember = "Products"; dgItems.DataKeyField = "CategoryID"; dgItems.DataBind(); dgItems.Visible = true; } else dgItems.Visible = false; }
Thanx in advance
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
•
•
Join Date: Sep 2007
Posts: 68
Reputation:
Solved Threads: 13
hii
Add the 3rd line code while binding DDlist.
Mark as sovled if it helps you!!!
Add the 3rd line code while binding DDlist.
ASP.NET Syntax (Toggle Plain Text)
ddlItems.DataSource = dsCategory; ddlItems.DataMember = "Categories"; ddlItems.DataValueField= "CategoryID" ddlItems.DataTextField = "Description"; ddlItems.DataS= "CategoryID";
Mark as sovled if it helps you!!!
Last edited by reach_yousuf; Dec 2nd, 2008 at 8:04 am.
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.
•
•
Join Date: Sep 2007
Posts: 68
Reputation:
Solved Threads: 13
As i understood from you code in selectindexchanged event:
You are passing param "categoryId", therefore u need to bind
dddl1.DataValueField = "categoryid"
Pls. let me know your exact concern
ASP.NET Syntax (Toggle Plain Text)
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
Try doing this.. it should work
Replace this code :
With
and also puut all your code under Page_Load like this
and in your SelectedIndexChanged function
change this
Replace this code :
asp.net Syntax (Toggle Plain Text)
ddlItems.DataSource = dsCategory; ddlItems.DataMember = "Categories"; ddlItems.DataTextField = "Description";
With
asp.net Syntax (Toggle Plain Text)
ddlItems.DataSource = dsCategory; ddlItems.DataValuefield = "CategoryId"; ddlItems.DataTextField = "CategoryName";
and also puut all your code under Page_Load like this
asp.net Syntax (Toggle Plain Text)
protected void Page_Load(object sender, EventArgs e) { if(!IsPostback) { //This code is filling in the Dropdownlist with data from Category table // your complete Dropdown code here } }
and in your SelectedIndexChanged function
change this
asp.net Syntax (Toggle Plain Text)
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... >>>
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
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)
/*Filling up the dropdownlist with each category. I used a procedure which select everything from Category which only have CategoryID and categoryDescription*/ protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string strCategory = "sdp_ViewCategory"; DataSet dsCategory = new DataSet(); SqlDataAdapter daCategory = new SqlDataAdapter(strCategory, conn); daCategory.Fill(dsCategory, "Categories"); ddlItems.DataSource = dsCategory; ddlItems.DataValueField = "CategoryID"; ddlItems.DataTextField = "Description"; ddlItems.DataBind(); ddlItems.Items.Insert(0, "Please select a category"); } } /*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*/ protected void ddlItems_SelectedIndexChanged(object sender, EventArgs e) { dgItems.Visible = true; string strItems = "sdp_ViewCartAndProduct"; SqlDataAdapter daItems = new SqlDataAdapter(strItems, conn); if (ddlItems.SelectedIndex > 0) { DataSet dsItems = new DataSet(); daItems.SelectCommand.CommandText = "Select ProductID, ProductName, PictureName, UnitsInStock from dbo.Products where CategoryID = " + ddlItems.SelectedValue; try { dsItems.Tables.Remove("Products"); } catch (Exception de) { lblMsg.Text = de.Message; } daItems.Fill(dsItems, "Products"); dgItems.DataSource = dsItems; dgItems.DataMember = "Products"; dgItems.DataBind(); dgItems.Visible = true; //dgItems.SelectedItem.Cells[4].Visible = false; foreach(DataGridColumn col in dgItems.Columns) { if (col.HeaderText == "PictureName") col.Visible = false; } } else dgItems.Visible = false; } /*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*/ protected void dgItems_SelectedIndexChanged(object sender, EventArgs e) { imgItems.Visible = true; imgItems.ImageUrl = "Pictures/" + dgItems.SelectedItem.Cells[4].Text; string strDetails = "sdp_ViewProducts"; DataSet dsDetails = new DataSet(); SqlDataAdapter daDetails = new SqlDataAdapter(strDetails, conn); daDetails.SelectCommand.CommandText = "Select ProductID, ProductName, UnitPrice from dbo.Products where ProductID = " + dgItems.SelectedItem.Cells[2].Text; daDetails.Fill(dsDetails, "Products"); dgDetails.DataSource = dsDetails; dgDetails.DataMember = "Products"; dgDetails.DataKeyField = "ProductID"; dgDetails.DataBind(); dgDetails.Visible = true; }
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.
•
•
Join Date: Dec 2008
Posts: 21
Reputation:
Solved Threads: 0
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
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
asp.net Syntax (Toggle Plain Text)
protected void Page_Load(object sender, EventArgs e) { Response.Write("sql is connected"); SqlDataAdapter da = new SqlDataAdapter("select countryid,countryname from countryname", cn); DataSet ds = new DataSet(); da.Fill(ds,"countryname"); ddlcountryname.DataSource = ds.Tables["countryname"]; ddlcountryname.DataTextField = "countryname"; ddlcountryname.DataValueField = "countryid"; ddlcountryname.DataBind(); } protected void ddlcountryname_SelectedIndexChanged(object sender, EventArgs e) { string country_id; country_id = ddlcountryname.SelectedValue.ToString(); SqlDataAdapter da1=new SqlDataAdapter("select stateid,statename from statename where countryid=' country_id'",cn); DataSet ds1 = new DataSet(); da1.Fill(ds1, "statename"); ddlstatename.DataSource = ds1.Tables["statename"]; ddlstatename.DataTextField = "statename"; ddlstatename.DataValueField = "stateid"; ddlstatename.DataBind(); }
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.
![]() |
Other Threads in the ASP.NET Forum
- Previous Thread: How to Send email using asp.net with C#
- Next Thread: how to print the contents of text box
| Thread Tools | Search this Thread |
.net 2.0 3.5 activexcontrol advice ajax alltypeofvideos asp asp.net bc30451 bottomasp.net browser businesslogiclayer button c# c#gridviewcolumn checkbox child click commonfunctions compatible confirmationcodegeneration content contenttype countryselector courier css dataaccesslayer database datagrid datagridview datagridviewcheckbox datalist deadlock development dgv dropdownlist dropdownmenu edit expose feedback flash flv form formatdecimal forms formview gridview homeedition hosting iframe iis javascript jquery list listbox login menu microsoft mono mouse mssql multistepregistration nameisnotdeclared news numerical objects order panelmasterpagebuttoncontrols radio ratings rotatepage save schoolproject search security serializesmo.table silverlight smartcard sql-server sqlserver2005 suse textbox tracking typeof unauthorized validation vb.net video videos virtualdirectory vista visual-studio visualstudio web webarchitecture webdevelopemnt webservice xml youareanotmemberofthedebuggerusers





