Click here to Skip to main content
12,700,957 members (30,593 online)
Rate this:
Please Sign up or sign in to vote.
See more: ASP.NET
I have a gridview which shows my products and I can Edit them if I choose Edit in my grid view;
I have two nested DropDownList with child-parent relation in EditItemTemplate in my gridview,the first DropDownList (parent) is related to my products brand and the second DropDownList (child) is related to my products Categories. I mean when user select one Item in in first DropDownList (brands) the Second DropDownList shows the list of categories which is related to selected brand.
relatede code in my gridview:
 <asp:TemplateField >
 < itemtemplate >
                    <asp:Label ID="brandname" runat="server"Text='<%#Eval("brand_name") %>'> 
                    <asp:DropDownList  ID="brandDrop" runat="server" DataTextField="brand_name" DataValueField="id" DataSourceID="SqlDataSource4" AutoPostBack="true"  >
                    <asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:mobile_storeConnectionString2 %>" SelectCommand="select [id],[brand_name] from [brands]" >

                    <asp:Label ID="catname" runat="server" Text='<%# Eval("category_name") %>'>
                   <asp:DropDownList ID="categoryDrop" runat="server" DataTextField="category_name" DataValueField="id" DataSourceID="SqlDataSource3" AutoPostBack="true">
                   <asp:SqlDataSource ID="SqlDataSource3" runat="server"   ConnectionString="<%$ ConnectionStrings:mobile_storeConnectionString2 %>" SelectCommand="select [category_name],[id],[idfrombrands] from [categories] where idfrombrands=@idfrombrands " >
                      <asp:ControlParameter ControlID="brandDrop" DefaultValue="1" 
                        Name="idfrombrands" PropertyName="SelectedValue" Type="Int32"  />


and this is sqldatasource for my gridview:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:mobile_storeConnectionString3 %>" 
        SelectCommand="SELECT * FROM [AdminProductList]" UpdateCommand="UPDATE AdminProductList SET p_name=@p_name,p_price=@p_price,p_qty=@p_qty,p_Img_S=@path ,idfromCategories=@cat_id,idfrombrands=@brand_id WHERE [id]=@id">
         <asp:Parameter Name="path" DefaultValue="default.gif" />
         <asp:Parameter Name="brand_id " Type="Int32"  DefaultValue="1"/>
         <asp:Parameter Name="cat_id " Type="Int32" DefaultValue="1"/>

My problem is I don't know how to get the selected value form dropdownlists, update my sqldatasource1 and data in tables in my database and if the user click on edit but did not select any item from dropdownlists (I mean the user did not edit brands and categories )then I want to store the old data of dropdownlists
I dont know what should I write in GridView1_RowDataBound and GridView1_RowUpdating

and this is my AdminProductList view in database that I use it in sqldatasource1 and my gridview use sqldatasource1:
SELECT AS brandId, AS categoryID, dbo.brands.brand_name, dbo.categories.category_name,, dbo.Products.p_name, 
                      dbo.Products.p_price, dbo.Products.p_qty, dbo.Products.p_Img_S, dbo.Products.idfromCategories, dbo.categories.idfrombrands
FROM         dbo.brands INNER JOIN
                      dbo.categories ON = dbo.categories.idfrombrands INNER JOIN
                      dbo.Products ON = dbo.Products.idfromCategories

This is my code for GridView1_RowUpdating and GridView1_RowDataBound:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

        DropDownList list1 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("brandDrop");
        //Label lbl = (Label)GridView1.Rows[e.RowIndex].FindControl("br");
        int NewIdFromBrand = -1; 
        if (list1.SelectedItem.Value != null)
             NewIdFromBrand = Convert.ToInt32(list1.SelectedItem.Value);
            SqlDataSource1.UpdateParameters["brand_id"].DefaultValue = NewIdFromBrand.ToString();


        DropDownList list2 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("categoryDrop");
       // Label lbl2 = (Label)GridView1.Rows[e.RowIndex].FindControl("cat");

        if (list2.SelectedItem.Value != null)
            SqlDataSource1.UpdateParameters["cat_id"].DefaultValue = list2.SelectedValue;



        string photoname = System.Guid.NewGuid().ToString();

        GridViewRow row = GridView1.Rows[e.RowIndex];
        FileUpload fileUpload = row.FindControl("FileUploadimg") as FileUpload;
        Label lbl3 = (Label)GridView1.Rows[e.RowIndex].FindControl("oldImage"); //(Label)GridView1.TemplateControl.FindControl("oldImage");
        if (fileUpload != null && fileUpload.HasFile)
            fileUpload.SaveAs(Server.MapPath("~/P_Image") + photoname + fileUpload.FileName);
            //   fileUpload.PostedFile.SaveAs(path + "/" + photoname +filename);
            SqlDataSource1.UpdateParameters["path"].DefaultValue = "~/P_Image" + photoname + fileUpload.FileName;

            SqlDataSource1.UpdateParameters["path"].DefaultValue = lbl3.Text;//oldImage.Text;

        GridView1.EditIndex = -1;


  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            //check if is in edit mode
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                DataRowView dRowView1 = (DataRowView)e.Row.DataItem;
                if (e.Row.RowType == DataControlRowType.DataRow)
                    if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                        DropDownList ddlStatus = (DropDownList)e.Row.FindControl("brandDrop");
                        ddlStatus.SelectedValue = dRowView1[0].ToString();
//dRowView1[0] gives me brandId column in my view
                        DropDownList ddlStatus2 = (DropDownList)e.Row.FindControl("categoryDrop");
                        ddlStatus2.SelectedValue = dRowView1[1].ToString();
//dRowView1[1] gives me categooryID column in my view


I don't know what is wrong with this code, but when I run this page this error is given:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:
Line 26:             NewIdFromBrand = Convert.ToInt32(list1.SelectedItem.Value);
Line 27:             Label1.Text = NewIdFromBrand.ToString();
Line 28:             SqlDataSource1.UpdateParameters["brand_id"].DefaultValue = NewIdFromBrand.ToString();
Line 29: 
Line 30:             //SqlDataSource1.UpdateParameters["brand_id"].DefaultValue = list1.SelectedValue;

I used this code in another page but with one DropDowwnList and it works fine.

if anyone help me I will be so thankfull
Posted 9-Dec-12 6:35am
Updated 12-Dec-12 2:04am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You need to do a server side binding for categoryDrop on onSelectedIndexChanged event of brandDrop combo box.

onSelectedIndexChanged event should look like
protected void categoryDrop_onSelectedIndexChanged(object sender, eventargs e)
     dropdownlist brandDrop =(dropdownList)sender;
     dropdownlist categoryDrop =e.item.findcontrol("categoryDrop");
     //select the datatable from db that use the brandDrop.selectedValue;
     // supportse the data table need to bind id dt
     datatable dt=getcatogory(brandDrop.selectedValue);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 12 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100