cascading dropdownlist in gridview edit mode

I am getting the following error for the sql select param.. Could not find control 'DDLProductCode' in ControlParameter 'product_code'.

Please find my code below.. I am using asp.net c#


<asp:GridView runat="server" DataSourceID="GVProductDS"  ID="GVAvailability" DataKeyNames="incident_id"
            AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" OnRowDataBound="GVAvailability_RowDataBound" OnRowUpdating="GVAvailability_RowUpdating" >
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="incident_id" HeaderText="incident_id" 
                    SortExpression="incident_id" InsertVisible="False" ReadOnly="True" />
                <asp:TemplateField HeaderText="product_code" SortExpression="product_code">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DDLProductCode" runat="server" DataSourceID="DDLProductDS" AutoPostBack= "true"
                            DataTextField="product_code" DataValueField="product_code" OnSelectedIndexChanged="DDLProductCode_OnSelectedIndexChanged">
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("product_code") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="platform_id" SortExpression="platform_id">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DDLPlatform" runat="server" 
                            DataSourceID="DDLPlatformDS" DataTextField="platform_id" 
                            DataValueField="platform_id">
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("platform_id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="incident_type" SortExpression="incident_type">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("incident_type") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("incident_type") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
<asp:SqlDataSource 
ID="DDLProductDS" 
runat="server" 
ConnectionString="<%$ ConnectionStrings:EMPortalConnectionString %>"

SelectCommand="SELECT DISTINCT product_name, [00_sla_product].product_code as product_code FROM 
[00_stage_availability_platforms], [00_sla_product] 
WHERE [00_stage_availability_platforms].product_code = [00_sla_product].product_code 
and [00_stage_availability_platforms].product_code not in (81,86) 
UNION 
SELECT 'Crossroads' as product_name, '00' as product_code 
UNION 
SELECT 'SMS-IG / SMS-IP' as product_name, '81' as product_code 
ORDER BY product_name 
"> 
</asp:SqlDataSource>            
<asp:SqlDataSource ID="DDLPlatformDS" runat="server" 
            ConnectionString="<%$ ConnectionStrings:EMPortalConnectionString %>" SelectCommand="
SELECT platform_name, platform_id
FROM [00_stage_availability_platforms]
WHERE product_code = @product_code">
   <SelectParameters>           
                <asp:controlparameter Name="product_code" controlid="DDLProductCode" DefaultValue="03"  Type="String" propertyname="SelectedValue"/>
            </SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="DDLTypeDS" runat="server" 
            ConnectionString="<%$ ConnectionStrings:EMPortalConnectionString %>" 
            SelectCommand="SELECT type_descr, TYPE_ID from [00_stage_availability_types]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="GVProductDS" runat="server"
            ConnectionString="<%$ ConnectionStrings:EMPortalConnectionString %>" DeleteCommand="DELETE FROM [EMPortal].[dbo].[00_stage_availability]
      WHERE incident_id=@incidentID" 
            SelectCommand="SELECT incident_id, [product_code], [platform_id], [incident_type] FROM [00_stage_availability]" 
            UpdateCommand="UPDATE [EMPortal].[dbo].[00_stage_availability]
   SET [product_code] = @product_code,
      ,[platform_id] = @platform_id,
      ,[incident_type] = @incident_type
 WHERE incident_id=@incidentID ">
 <UpdateParameters>
                <asp:Parameter Name="product_code" />
                <asp:Parameter Name="platform_id" />
                <asp:Parameter Name="incident_type" />
            </UpdateParameters>
 </asp:SqlDataSource>

public partial class ServiceAvailabilityMaintainence : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void GVAvailability_RowDataBound(object sender, GridViewRowEventArgs e)
    {


        if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
        {
            DataRowView dv = (DataRowView)e.Row.DataItem;

            //// Preselect correct value in product list
            DropDownList DDLProductCode = (DropDownList)e.Row.FindControl("DDLProductCode");


            DDLProductCode.SelectedValue = dv["product_code"].ToString();

            // Databind list of platforms in dependent drop-down list
            DropDownList DDLPlatform = (DropDownList)e.Row.FindControl("DDLPlatform");
            SqlDataSource dsc = (SqlDataSource)e.Row.FindControl("DDLPlatformDS");
            if(dsc!=null)
            dsc.SelectParameters["product_code"].DefaultValue = dv["product_code"].ToString();

            DDLPlatform.DataBind();
            DDLPlatform.SelectedValue = (dv["platform_id"].ToString());

        }
    }
    protected void DDLProductCode_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList DDLProductCode;
        DropDownList DDLPlatform;
        SqlDataSource DDLPlatformDS;
        int currentRowInEdit = GVAvailability.EditIndex;
        DDLProductCode = (DropDownList)sender;
        DDLPlatform = (DropDownList)GVAvailability.Rows[currentRowInEdit].FindControl("DDLPlatform");
        DDLPlatformDS = (SqlDataSource)GVAvailability.Rows[currentRowInEdit].FindControl("DDLPlatformDS");
        DDLPlatformDS.SelectParameters["product_code"].DefaultValue = DDLProductCode.SelectedValue;
        DDLPlatform.DataBind();
    }

Open in new window

vdyatAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CmdoProg2Commented:
If you are loading the parameter value in the code-behind, do you not need to bind the control in DDLPlatformDS.  
Try
                <asp:Parameter Name="product_code" DefaultValue="03" Type="String" />

  Generally, I use AJAX cascading dropdownlist within the same cell for this situation where binding to the SelectedValue is needed for both product_code and platform_id for the update function.
0
vdyatAuthor Commented:
@cmdoProg2: thank you for replying... If I put the prameter the datasource is returning a null value when I try to reference it.
SqlDataSource dsc = (SqlDataSource)e.Row.FindControl("DDLPlatformDS");
          In the above line dsc is returning null value which inturn is making
   dsc.SelectParameters["product_code"].DefaultValue = dv["product_code"].ToString();
 throw an error
'Object reference not set to an instance of an object.'

I am not sure wher I am going wrong!
0
CmdoProg2Commented:
DDLPlatformDS is not part of the gridview.  It can be directly referenced...
protected void GVAvailability_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
        {
            DataRowView dv = (DataRowView)e.Row.DataItem;

            //// Preselect correct value in product list
            DropDownList DDLProductCode = (DropDownList)e.Row.FindControl("DDLProductCode");


            DDLProductCode.SelectedValue = dv["product_code"].ToString();

            // Databind list of platforms in dependent drop-down list
            DDLPlatformDS.SelectParameters["product_code"].DefaultValue = dv["product_code"].ToString();
            DropDownList DDLPlatform = (DropDownList)e.Row.FindControl("DDLPlatform");
            DDLPlatform.DataBind();
            DDLPlatform.SelectedValue = (dv["platform_id"].ToString());

        }
    }
    protected void DDLProductCode_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList DDLProductCode;
        DropDownList DDLPlatform;
        int currentRowInEdit = GVAvailability.EditIndex;
        DDLProductCode = (DropDownList)sender;
        DDLPlatform = (DropDownList)GVAvailability.Rows[currentRowInEdit].FindControl("DDLPlatform");
        DDLPlatformDS.SelectParameters["product_code"].DefaultValue = DDLProductCode.SelectedValue;
        DDLPlatform.DataBind();
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CmdoProg2Commented:
This snippet should do the cascade and update with no code behind.  You may want to take a look at the update Parameters
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ServiceAvailabilityMaintainence.aspx.cs" Inherits="ServiceAvailabilityMaintainence" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView runat="server" DataSourceID="GVProductDS" ID="GVAvailability" DataKeyNames="incident_id" AutoGenerateColumns="False"
            AllowPaging="True" AllowSorting="True" >
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="incident_id" HeaderText="incident_id" SortExpression="incident_id" InsertVisible="False" ReadOnly="True" />
                <asp:TemplateField HeaderText="product_code/ platform_id" SortExpression="product_code">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DDLProductCode" runat="server" DataSourceID="DDLProductDS" AutoPostBack="True" DataTextField="product_code"
                            DataValueField="product_code" SelectedValue='<%# Bind("product_code") %>'>
                        </asp:DropDownList>
                        <asp:DropDownList ID="DDLPlatform" runat="server" DataSourceID="DDLPlatformDS" DataTextField="platform_id" DataValueField="platform_id"
                            SelectedValue='<%# Bind("platform_id") %>'>
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="DDLPlatformDS" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="
    SELECT platform_name, platform_id
    FROM [00_stage_availability_platforms]
    WHERE product_code = @product_code">
                            <SelectParameters>
                                <asp:ControlParameter ControlID="DDLProductCode" DefaultValue="03" Name="product_code" PropertyName="SelectedValue" Type="String" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("product_code") %>'></asp:Label>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("platform_id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="DDLProductDS" runat="server" ConnectionString="<%$ ConnectionStrings:EMPortalConnectionString %>"
            SelectCommand="SELECT DISTINCT product_name, [00_sla_product].product_code as product_code FROM 
    [00_stage_availability_platforms], [00_sla_product] 
    WHERE [00_stage_availability_platforms].product_code = [00_sla_product].product_code 
    and [00_stage_availability_platforms].product_code not in (81,86) 
    UNION 
    SELECT 'Crossroads' as product_name, '00' as product_code 
    UNION 
    SELECT 'SMS-IG / SMS-IP' as product_name, '81' as product_code 
    ORDER BY product_name 
    "></asp:SqlDataSource>
        <asp:SqlDataSource ID="DDLTypeDS" runat="server" ConnectionString="<%$ ConnectionStrings:EMPortalConnectionString %>" SelectCommand="SELECT type_descr, TYPE_ID from [00_stage_availability_types]">
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="GVProductDS" runat="server" ConnectionString="<%$ ConnectionStrings:EMPortalConnectionString %>" DeleteCommand="DELETE FROM [EMPortal].[dbo].[00_stage_availability]
          WHERE incident_id=@incidentID" SelectCommand="SELECT incident_id, [product_code], [platform_id], [incident_type] FROM [00_stage_availability]"
            UpdateCommand="UPDATE [EMPortal].[dbo].[00_stage_availability]
       SET [product_code] = @product_code,
          ,[platform_id] = @platform_id,
          ,[incident_type] = @incident_type
     WHERE incident_id=@incidentID ">
            <UpdateParameters>
                <asp:Parameter Name="incident_id" />
                <asp:Parameter Name="product_code" />
                <asp:Parameter Name="platform_id" />
                <asp:Parameter Name="incident_type" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

Open in new window

0
vdyatAuthor Commented:
Thanks a lot CmdoProg2!! your previous solution worked for me... I will take a look into this also! ..  You saved me a lot of trouble! thanks again!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.