0

I am try to using four dropdown lists in cascading mode in GridView for data editing. I have successfully implementing the cascading feature in three lists but fourth list is not working. My aspx page along with VB code is

<%@ Page Language="VB"  MaintainScrollPositionOnPostback="true" Debug="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<style>
body
{ font-family:verdana;
font-size:10pt;
}
</style>

<script runat="server">

Protected Sub ddlclassification_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim ddlclassification, ddlbase, ddlLocoType, ddlEquipment As DropDownList
    Dim ShedBYClassification As SqlDataSource
    Dim LocoTypeBYShed As SqlDataSource
    Dim EquipBYLocoType As SqlDataSource
    Dim currentRowInEdit As Integer = GridView1.EditIndex
    ddlclassification = CType(sender, DropDownList)
    ddlbase = CType(GridView1.Rows(currentRowInEdit).FindControl("ddlbase"), DropDownList)
    ddlLocoType = CType(GridView1.Rows(currentRowInEdit).FindControl("ddlLocoType"), DropDownList)
    ddlEquipment = CType(GridView1.Rows(currentRowInEdit).FindControl("ddlEquipment"), DropDownList)
    ShedBYClassification = CType(GridView1.Rows(currentRowInEdit).FindControl("ShedBYClassification"), SqlDataSource)
    ShedBYClassification.SelectParameters("ClassificationName").DefaultValue = ddlclassification.SelectedValue
    LocoTypeBYShed = CType(GridView1.Rows(currentRowInEdit).FindControl("LocoTypeBYShed"), SqlDataSource)
    LocoTypeBYShed.SelectParameters("ClassificationName").DefaultValue = ddlclassification.SelectedValue
    EquipBYLocoType = CType(GridView1.Rows(currentRowInEdit).FindControl("EquipBYLocoType"), SqlDataSource)
    EquipBYLocoType.SelectParameters("LocoTypeName").DefaultValue = ddlLocoType.SelectedValue
    ddlbase.DataBind()
    ddlLocoType.DataBind()
    ddlEquipment.DataBind()
    
End Sub

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
    If (e.Row.RowState And DataControlRowState.Edit) = DataControlRowState.Edit Then
        Dim dv As System.Data.DataRowView = e.Row.DataItem
        Dim ddlclassification As DropDownList = e.Row.FindControl("ddlclassification")
        ddlclassification.SelectedValue = dv("classificationName")
        Dim ddlbase As DropDownList = e.Row.FindControl("ddlbase")
        Dim ddlLocoType As DropDownList = e.Row.FindControl("ddlLocoType")
        Dim ddlEquipment As DropDownList = e.Row.FindControl("ddlEquipment")
        Dim dsc As SqlDataSource = e.Row.FindControl("ShedBYClassification")
        Dim dsclocotype As SqlDataSource = e.Row.FindControl("LocoTypeBYShed")
        Dim dscEquipment As SqlDataSource = e.Row.FindControl("EquipBYLocoType")
        dsc.SelectParameters("ClassificationName").DefaultValue = dv("classificationName")
        dsclocotype.SelectParameters("ClassificationName").DefaultValue = dv("ClassificationName")
        dscEquipment.SelectParameters("LocoTypeName").DefaultValue = dv("LocoTypeName")
        ddlbase.DataBind()
        ddlLocoType.DataBind()
        ddlEquipment.DataBind()
        ddlbase.SelectedValue = dv("Shed")
        ddlLocoType.SelectedValue = dv("LocoTypeName")
        ddlEquipment.SelectedValue = dv("EquipName")
    End If
End Sub

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)
    Dim ddlclassification As DropDownList = CType(GridView1.Rows(e.RowIndex).FindControl("ddlclassification"), DropDownList)
    Dim ddlbase As DropDownList = CType(GridView1.Rows(e.RowIndex).FindControl("ddlbase"), DropDownList)
    Dim ddlLocoType As DropDownList = CType(GridView1.Rows(e.RowIndex).FindControl("ddlLocoType"), DropDownList)
    Dim ddlEquipment As DropDownList = CType(GridView1.Rows(e.RowIndex).FindControl("ddlEquipment"), DropDownList)
    e.NewValues("classification") = ddlclassification.SelectedValue
    e.NewValues("Shed") = ddlbase.SelectedValue
    e.NewValues("loco_type") = ddlLocoType.SelectedValue
    e.NewValues("Equipt") = ddlEquipment.SelectedValue
End Sub

</script>

 <html xmlns="http://www.w3.org/1999/xhtml">
 <head id="Head1" runat="server">
<title>Cascading DropDownList Controls in a GridView Edit Mode</title>
 </head>
<body>
<form id="form1" runat="server">
    <div>
        <div>TEST</div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="ID" DataSourceID="SqlDataSource1"
            HorizontalAlign="Center" AllowSorting="True" OnRowDataBound="GridView1_RowDataBound" OnRowUpdating="GridView1_RowUpdating"> 

            <Columns>
                <asp:CommandField ShowDeleteButton="False" ShowEditButton="True" />
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
                    ReadOnly="True" SortExpression="ID" Visible="false" />
                
                <asp:TemplateField HeaderText="Date" SortExpression="Date">
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server"
                            Text='<%# Bind("Date", "{0:dd-MMM-yy}") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:Label ID="TextBox3" runat="server" Text='<%# Bind("Date", "{0:MM/dd/yyyy}") %>' ></asp:Label>
                    </EditItemTemplate>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="False" />
                </asp:TemplateField>
                
                <asp:TemplateField HeaderText="Classification of Case" SortExpression="classification">
                    <ItemTemplate>
                        <asp:Label ID="Label9" runat="server" Text='<%# Bind("classification") %>'></asp:Label>
                    </ItemTemplate>
                     <EditItemTemplate>
                           <asp:DropDownList ID="ddlclassification" runat="server" DataSourceID="ClassificationDDL" DataTextField="classificationName"
                            DataValueField="classificationName" AutoPostBack="true" OnSelectedIndexChanged="ddlclassification_SelectedIndexChanged">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="ClassificationDDL" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:EquipfailuresERConnectionString  %>" 
                    SelectCommand="SELECT [ClassificationName] FROM [ClassificationDDL] ORDER BY [ClassificationName]" 
                    EnableCaching="True">
                </asp:SqlDataSource>
                        </EditItemTemplate>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="True" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Loco no." SortExpression="Loco">
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Text='<%# Bind("Loco") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Loco") %>' ></asp:TextBox>
                        
                    </EditItemTemplate>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" 
                        Wrap="True" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Home Rly" SortExpression="Homerly">
                    <ItemTemplate>
                        <asp:Label ID="Label7" runat="server" Text='<%# Bind("Homerly") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:Label ID="ddlRly" runat="server" Text='<%# Bind("Homerly") %>'></asp:Label>
                        
                    </EditItemTemplate>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="True" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Shed" SortExpression="Shed">
                    <ItemTemplate>
                        <asp:Label ID="Label6" runat="server" Text='<%# Bind("Shed") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlbase" runat="server" DataSourceID="ShedBYClassification" DataTextField="ShedName"  DataValueField="ShedName" AutoPostBack="true" />
                        <asp:SqlDataSource ID="ShedBYClassification" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:EquipfailuresERConnectionString %>" 
                    SelectCommand="SELECT [ShedID], [ShedName] FROM [ShedDDL] WHERE ([ClassificationID] = @ClassificationName) ORDER BY ShedName" EnableCaching="True">
                    <SelectParameters>
                        <asp:Parameter Name="ClassificationName" />
                    </SelectParameters>
                            </asp:SqlDataSource>             
                    </EditItemTemplate>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="True" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Loco Type" SortExpression="loco_type">
                    <ItemTemplate>
                        <asp:Label ID="Label11" runat="server" Text='<%# Bind("loco_type") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlLocoType" runat="server" DataSourceID="LocoTypeBYShed" DataTextField="LocoTypeName"  DataValueField="LocoTypeName"
                             AutoPostBack="true" />
                        <asp:SqlDataSource ID="LocoTypeBYShed" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:EquipfailuresERConnectionString %>" 
                    SelectCommand="SELECT [LocoTypeID], [LocoTypeName] FROM [LocoTypeDDL] WHERE ([ClassificationID] = @ClassificationName) ORDER BY LocoTypeName" EnableCaching="True">
                    <SelectParameters>
                        <asp:Parameter Name="ClassificationName" />
                    </SelectParameters>
                            </asp:SqlDataSource>
                    </EditItemTemplate>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="True" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Equipment" SortExpression="Equipt">
                    <ItemTemplate>
                        <asp:Label ID="Label8" runat="server" Text='<%# Bind("Equipt") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlEquipment" runat="server" DataSourceID="EquipBYLocoType" DataTextField="EquipName"  DataValueField="EquipName"
                             AutoPostBack="true"  />
                        <asp:SqlDataSource ID="EquipBYLocoType" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:EquipfailuresERConnectionString %>" 
                    SelectCommand="SELECT [EquipID], [EquipName] FROM [EquipmentDDL] WHERE ([LocoTypeID] = @LocoTypeName) ORDER BY EquipName" EnableCaching="True">
                    <SelectParameters>
                        <asp:Parameter Name="LocoTypeName" />
                    </SelectParameters>
                            </asp:SqlDataSource>

                        </EditItemTemplate>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="True" />
                </asp:TemplateField>
                </Columns>
            <FooterStyle Font-Bold="True" Font-Names="Tahoma"
                Font-Size="Medium" HorizontalAlign="Center" VerticalAlign="Middle" />
            <HeaderStyle Font-Names="Tahoma"
                Font-Size="Small" />
            <RowStyle Font-Names="Tahoma" Font-Size="Small" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:EquipfailuresERConnectionString %>"
            SelectCommand="TESTINNER" SelectCommandType="StoredProcedure" UpdateCommand="EnRRlyupdate" UpdateCommandType="StoredProcedure">
            
        </asp:SqlDataSource>
                    <br />
    </div>
</form>
</body>
</html>

SQL data for Dropdown lists is as under:

1st Dropdown List - 'ClassificationDDL' SQL table

  +------------------------+------------------------+
  |    ClassificationID    |   ClassificationName   |
  +:----------------------:+:----------------------:+
  | Loco                   | Loco                   |
  | Crew/Loco Operation    | Crew/Loco Operation    |
  | Traffic/wrongly logged | Traffic/wrongly logged |
  | Stalling               | Stalling               |
  +------------------------+------------------------+

2nd Dropdown List - 'ShedDDL' SQL table

  +------------------------+----------+--------+
  |    ClassificationID    | ShedName | ShedID |
  +:----------------------:+:--------:+:------:+
  | Loco                   | BSL      | BSL    |
  | Loco                   | AQ       | AQ     |
  | Loco                   | KYN      | KYN    |
  | Stalling               | Other    | Other  |
  | Stalling               | BSL      | BSL    |
  | Stalling               | AQ       | AQ     |
  | Stalling               | KYN      | KYN    |
  | Crew/Loco Operation    | Other    | Other  |
  | Crew/Loco Operation    | BSL      | BSL    |
  | Crew/Loco Operation    | AQ       | AQ     |
  | Crew/Loco Operation    | KYN      | KYN    |
  | Traffic/wrongly logged | BSL      | BSL    |
  | Traffic/wrongly logged | AQ       | AQ     |
  | Traffic/wrongly logged | KYN      | KYN    |
  | Traffic/wrongly logged | Other    | Other  |
  +------------------------+----------+--------+

3rd Dropdown list - 'LocoTypeDDL' SQL table

  +------------------------+--------------+--------------+
  |    ClassificationID    |  LocoTypeID  | LocoTypeName |
  +:----------------------:+:------------:+:------------:+
  | Loco                   | Conventional | Conventional |
  | Loco                   | 3-Phase      | 3-Phase      |
  | Crew/Loco Operation    | Conventional | Conventional |
  | Crew/Loco Operation    | 3-Phase      | 3-Phase      |
  | Crew/Loco Operation    | Other        | Other        |
  | Traffic/wrongly logged | Conventional | Conventional |
  | Traffic/wrongly logged | 3-Phase      | 3-Phase      |
  | Traffic/wrongly logged | Other        | Other        |
  | Stalling               | Conventional | Conventional |
  | Stalling               | 3-Phase      | 3-Phase      |
  | Stalling               | Other        | Other        |
  +------------------------+--------------+--------------+

4th Dropdown list - 'EquipmentDDL' SQL Table

  +--------------+---------------------------+---------------------------+
  |  LocoTypeID  |         EquipName         |          EquipID          |
  +:------------:+:-------------------------:+:-------------------------:+
  | Other        | Other                     | Other                     |
  | Conventional | Air Blast Circuit Breaker | Air Blast Circuit Breaker |
  | Conventional | Air Dryer                 | Air Dryer                 |
  | Conventional | Arno                      | Arno                      |
  | Conventional | Aux. compressor           | Aux. compressor           |
  | Conventional | Auxiliary Motor           | Auxiliary Motor           |
  | Conventional | Axle Box                  | Axle Box                  |
  | Conventional | Battery                   | Battery                   |
  | 3-Phase      | 110V MCB                  | 110V MCB                  |
  | 3-Phase      | 415V  MCB                 | 415V  MCB                 |
  | 3-Phase      | Air Dryer                 | Air Dryer                 |
  | 3-Phase      | Angle transmitter         | Angle transmitter         |
  | 3-Phase      | Aux. compressor           | Aux. compressor           |
  | 3-Phase      | Aux. converter GTO (BUR)  | Aux. converter GTO (BUR)  |
  +--------------+---------------------------+---------------------------+

Select Command (stored procedure 'TESTINNER') is

  Select ERfailures.ID, ERfailures.Date, ERfailures.Loco, ERfailures.Shed, ERfailures.Homerly, ERfailures.Equipt, ERfailures.classification, ERfailures.Sub_head, ERfailures.loco_type, ClassificationDDL.ClassificationName, ShedDDL.ShedName, LocoTypeDDL.LocoTypeName, EquipmentDDL.EquipName from ERfailures 
  INNER JOIN ClassificationDDL ON ERfailures.classification = ClassificationDDL.ClassificationName
  INNER JOIN ShedDDL ON ERfailures.Shed = ShedDDL.ShedName AND ERfailures.classification = ShedDDL.ClassificationID
  INNER JOIN LocoTypeDDL ON ERfailures.loco_type = LocoTypeDDL.LocoTypeName AND ERfailures.classification =  LocoTypeDDL.ClassificationID
  INNER JOIN EquipmentDDL ON ERfailures.Equipt = EquipmentDDL.EquipName AND ERfailures.loco_type = EquipmentDDL.LocoTypeID

Update Command (stored procedure) 'EnRRlyupdate' is

  UPDATE ERfailures SET Date = @Date, Loco = @Loco, Shed = @Shed, Homerly = (CASE WHEN @Shed ='BSL' THEN 'CR' WHEN @Shed ='AQ' THEN 'ER' WHEN @Shed ='KYN' THEN 'ECR' ELSE 'xyz' END), Equipt = @Equipt, classification = @classification, loco_type = @loco_type WHERE ID = @ID

2nd Dropdown (ddlbase) & 3rd Dropdown (ddlLocoType) are based on the selected value in 1st dropdown list (ddlclassification). 4th dropdown list (ddlEquipment) is based on selected value in 3rd Dropdown list. Dropdown 1st, 2nd & 3rd are working fine but 4th dropdown is not working (means values not displaying according to value selected in 3rd Dropdown list. Please help for making this project workable.

user1185088
  • 99
  • 1
  • 1
  • 8
  • Based on what you've said here, my first guess would be the SqlCommand for ddlEquipment is not producing the expected results. Have you checked this query via tools like SSMS? – Hursey Oct 05 '21 at 19:51
  • @Hursey Sir, checked in SSMS, it is working as desired. The problem is that selected value of ddlLocoType Dropdown is not triggering the ddlEquipment dropdown to change the list of equipments. – user1185088 Oct 06 '21 at 11:03

0 Answers0