Link to home
Start Free TrialLog in
Avatar of vdyat
vdyat

asked on

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

Avatar of CmdoProg2
CmdoProg2
Flag of United States of America image

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.
Avatar of vdyat
vdyat

ASKER

@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!
ASKER CERTIFIED SOLUTION
Avatar of CmdoProg2
CmdoProg2
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of vdyat

ASKER

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!!