[Solved] ASP.Net Error: dropdownyear has a SelectedValue which is invalid because it does not exist in the list of items

Sumeet
 
on Aug 31, 2021 04:47 AM
411 Views

Sir,

Having a gridview with a multiple columns. Among those cols 1 column has dropdownlist. It has to fill with Session year of last five years as "--Select--", "2016-2017", "2017-2018", "2018-2019", "2019-2020", "2020-2021" (also with same values as items). 

Dropdownlist having selectedValue property that binded with database.

it's giving error on binding as 

"System.ArgumentOutOfRangeException: 'dropdownyear' has a SelectedValue which is invalid because it does not exist in the list of items. "

 HTML: 

<asp:GridView ID="gridviewfees" ShowHeaderWhenEmpty="true" runat="server" DataKeyNames="id" runat="server" AutoGenerateColumns="False" ShowFooter="true" HeaderStyle-Font-Size="14px" HeaderStyle-Font-Bold="true" PageSize="5" Width="100%" OnRowDataBound="gridviewfees_RowDataBound">
    <FooterStyle CssClass="feegridviewfooter" />
     
     <Columns>
       <asp:TemplateField>
         <HeaderTemplate>                                                                    
           <tr class="gvHeader">
              <th>Year</th>
              <th colspan="4"><font color="Red">Primary</font></th>                       
              <th colspan="4"><font color="Red">Middle</font></th>
              <th colspan="4"><font color="Red">Secondary</font></th>
              <th colspan="4"><font color="Red">Higher Secondary</font></th>
              <th></th>
           </tr>
           <tr class="gvHeader">
             <th></th>                                                       
             <th>Tution Fee<br/>(Rs.)</th>
             <th>Annual Fee<br/>(Rs.)</th>
             <th>Transport Fee<br/>(Rs.)</th>
             <th>Other Fee<br/>(Rs.)</th>
 
             <th>Tution Fee<br/>(Rs.)</th>
             <th>Annual Fee<br/>(Rs.)</th>
             <th>Transport Fee<br/>(Rs.)</th>
             <th>Other Fee<br/>(Rs.)</th>
              
             <th>Tution Fee<br/>(Rs.)</th>
             <th>Annual Fee<br/>(Rs.)</th>
             <th>Transport Fee<br/>(Rs.)</th>
             <th>Other Fee<br/>(Rs.)</th>
              
             <th>Tution Fee<br/>(Rs.)</th>
             <th>Annual Fee<br/>(Rs.)</th>
             <th>Transport Fee<br/>(Rs.)</th>
             <th>Other Fee<br/>(Rs.)</th>
              
             <th></th>
           </tr>
         </HeaderTemplate>
         <ItemTemplate>
             <tr class="gvHeader">
              
           <td>
               <asp:DropDownList ID="dropdownyear" runat="server" Enabled="false" SelectedValue='<%#  Server.HtmlEncode(Eval("for_year").ToString())%>' Width="130px">
                   <%--<asp:ListItem Value="0">--Select--</asp:ListItem>
                    
                   <asp:ListItem Value="2015-2016">2015-2016</asp:ListItem>
                   <asp:ListItem Value="2016-2017">2016-2017</asp:ListItem>
                   <asp:ListItem Value="2017-2018">2017-2018</asp:ListItem>
                   <asp:ListItem Value="2018-2019">2018-2019</asp:ListItem>
                   <asp:ListItem Value="2019-2020">2019-2020</asp:ListItem>--%>
               </asp:DropDownList>
           </td>
           
           <td><asp:Label ID="textboxptu" runat="server" Text='<%#  Server.HtmlEncode(Eval("p_tutionfee").ToString())%>'></asp:Label></td>
           <td><asp:Label ID="textboxpa" runat="server" Text='<%#  Server.HtmlEncode(Eval("p_annualfee").ToString())%>'></asp:Label></td>
           <td><asp:Label ID="textboxptr" runat="server" Text='<%# Server.HtmlEncode(Eval("p_transportfee").ToString())%>'></asp:Label></td>
           <td><asp:Label ID="textboxpo" runat="server" Text='<%#  Server.HtmlEncode(Eval("p_otherfee").ToString())%>'></asp:Label></td>
 
           <td><asp:Label ID="textboxmtu" runat="server" Text='<%# Server.HtmlEncode(Eval("m_tutionfee").ToString())%>'></asp:Label></td>
           <td><asp:Label ID="textboxma" runat="server" Text='<%#  Server.HtmlEncode(Eval("m_annualfee").ToString())%>'></asp:Label></td>
           <td><asp:Label ID="textboxmtr" runat="server" Text='<%# Server.HtmlEncode(Eval("m_transportfee").ToString())%>'></asp:Label></td>
           <td><asp:Label ID="textboxmo" runat="server" Text='<%#  Server.HtmlEncode(Eval("m_otherfee").ToString())%>'></asp:Label></td>
 
           <td><asp:Label ID="textboxstu" runat="server" Text='<%# Server.HtmlEncode(Eval("s_tutionfee").ToString())%>'></asp:Label></td>
           <td><asp:Label ID="textboxsa" runat="server" Text='<%#  Server.HtmlEncode(Eval("s_annualfee").ToString())%>'></asp:Label></td>
           <td><asp:Label ID="textboxstr" runat="server" Text='<%# Server.HtmlEncode(Eval("s_transportfee").ToString())%>'></asp:Label></td>
           <td><asp:Label ID="textboxso" runat="server" Text='<%#  Server.HtmlEncode(Eval("s_otherfee").ToString())%>'></asp:Label></td>
            
           <td><asp:Label ID="textboxhtu" runat="server" Text='<%# Server.HtmlEncode(Eval("h_tutionfee").ToString())%>'></asp:Label></td>
           <td><asp:Label ID="textboxha" runat="server" Text='<%# Server.HtmlEncode(Eval("h_annualfee").ToString())%>'></asp:Label></td>
           <td><asp:Label ID="textboxhtr" runat="server" Text='<%# Server.HtmlEncode(Eval("h_transportfee").ToString())%>'></asp:Label></td>
           <td><asp:Label ID="textboxho" runat="server" Text='<%#  Server.HtmlEncode(Eval("h_otherfee").ToString())%>'></asp:Label></td>
           <td><asp:Button ID="btnfeeedit" CommandName="Edit" runat="server" Text="Edit"></asp:Button></td>
             </tr>
         </ItemTemplate>
 
         <EditItemTemplate>
           <tr>
            <td>
               <asp:DropDownList ID="dropdownedityear" Enabled="false" runat="server" SelectedValue='<%#  Server.HtmlEncode(Eval("for_year").ToString())%>' Width="130px">
                   <%--<asp:ListItem Value="0">--Select--</asp:ListItem>
                   <asp:ListItem Value="2015-2016">2015-2016</asp:ListItem>
                   <asp:ListItem Value="2016-2017">2016-2017</asp:ListItem>
                   <asp:ListItem Value="2017-2018">2017-2018</asp:ListItem>
                   <asp:ListItem Value="2018-2019">2018-2019</asp:ListItem>
                   <asp:ListItem Value="2019-2020">2019-2020</asp:ListItem>--%>
               </asp:DropDownList> 
           </td>
           <td>
               <asp:Button ID="btnfeeupdate" CommandName="Update" runat="server" Text="Update" ValidationGroup="editfee"></asp:Button>&nbsp;
               <asp:Button ID="btnfeecancel" CommandName="Cancel" runat="server" Text="Cancel" CausesValidation="false"></asp:Button>
           </td>
          </tr>
         </EditItemTemplate>
         <FooterTemplate>
          <tr>
           <td>
               <asp:DropDownList ID="dropdowninsyear" runat="server" Width="130px">
                   <%--<asp:ListItem Value="0">Select</asp:ListItem>
                    
                   <asp:ListItem Value="2015-2016">2015-2016</asp:ListItem>
                   <asp:ListItem Value="2016-2017">2016-2017</asp:ListItem>
                   <asp:ListItem Value="2017-2018">2017-2018</asp:ListItem>
                   <asp:ListItem Value="2018-2019">2018-2019</asp:ListItem>
                   <asp:ListItem Value="2019-2020">2019-2020</asp:ListItem>--%>
                     
                   <%--<asp:ListItem Value="2020-2021">2020-2021</asp:ListItem>--%>
               </asp:DropDownList>
               <asp:RequiredFieldValidator ID="RequiredFieldValidator1dropdowninsyear" ControlToValidate="dropdowninsyear" runat="server" ErrorMessage="*Required" CssClass="errmsg" Display="Dynamic" SetFocusOnError="true"></asp:RequiredFieldValidator>                 
           </td>
          </tr>            
         </FooterTemplate>
       </asp:TemplateField>
     </Columns>
   </asp:GridView>

 

Protected Sub gridviewfees_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    Dim startyear As Integer = Now.Year - 5
    Dim endyear As Integer = Now.Year
    If (e.Row.RowType = DataControlRowType.Footer Or e.Row.RowType = DataControlRowType.DataRow) Then
 
        'Find the DropDownList in the Row
 
        Dim ddlYears As DropDownList = CType(e.Row.FindControl("dropdownyear"), DropDownList)
        Dim EditddlYears As DropDownList = CType(e.Row.FindControl("dropdownedityear"), DropDownList)
        Dim FooterddlYears As DropDownList = CType(e.Row.FindControl("dropdowninsyear"), DropDownList)
        'EditddlYears.Items.Clear()
        'FooterddlYears.Items.Clear()
 
        Dim str As String = Nothing
        Dim list As New List(Of String)
        For i As Integer = 0 To 4
            str = startyear & "-" & startyear + 1
            ddlYears.Items.Add(str)
            ddlYears.Items(i).Value = str
            EditddlYears.Items.Add(str)
            EditddlYears.Items(i).Value = str
            FooterddlYears.Items.Add(str)
            FooterddlYears.Items(i).Value = str
        Next i
 
 
        ''Add Default Item in the DropDownList
        EditddlYears.Items.Insert(0, New ListItem("--Select--"))
        FooterddlYears.Items.Insert(0, New ListItem("--Select--"))
 
    End If
End Sub

Sub loadfees()
    Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("dsejConnectionString").ConnectionString)
        Try
            Using cmda As New SqlDataAdapter("usp_commandtosps", conn)
                cmda.SelectCommand.CommandType = CommandType.StoredProcedure
                cmda.SelectCommand.Parameters.AddWithValue("@flag", vdata.remove_bad_words(20))
                cmda.SelectCommand.Parameters.AddWithValue("@ip", Session("ip").ToString())
                cmda.SelectCommand.Parameters.AddWithValue("@regid", vdata.remove_bad_words(Session("regid")))
                conn.Close()
                conn.Open()
                Dim ds As New DataSet
                cmda.Fill(ds)
                If ds.Tables(0).Rows.Count > 0 Then
                    If ds.Tables(0).Rows(0)("regid").ToString = "0" Then
                        'ds.Tables(0).Rows.Add(ds.Tables(0).NewRow())
                        gridviewfees.DataSource = ds
                        gridviewfees.DataBind()
                        gridviewfees.Rows(0).Visible = False
                    Else
                        gridviewfees.DataSource = ds.Tables(0).DefaultView
                        gridviewfees.DataBind()
                    End If
 
                Else
                    ds.Tables(0).Rows.Add(ds.Tables(0).NewRow())
                    gridviewfees.DataSource = ds
                    gridviewfees.DataBind()
                    gridviewfees.Rows(0).Visible = False
                End If
            End Using
        Catch ex As Exception
            Response.Write(ex.ToString())
        Finally
            If ConnectionState.Open Then
                conn.Close()
            End If
        End Try
    End Using
End Sub

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download