0

I am in a weird situation. I get "Object reference not set to an instance of an object" error for a dropdownlist which is present in my pagerrow of gridview. The weird thing is I do not get this error everytime, but only on some unfortunate times. I have tried debugging the situation when it shoots this error but have failed.

Here is my html

    <asp:GridView ID="gvVendor" runat="server" Width="100%" AutoGenerateColumns="False" AllowPaging="true"  PageSize="10" AllowSorting="true"
                  CssClass="mGrid" ShowFooter="False" CellSpacing="0" CellPadding="0" EnableTheming="false" SelectedRowStyle-CssClass="DashboardDataGridSelectedItem" DataKeyNames="ev_no" OnDataBound="gvVendor_DataBound" >
                  <AlternatingRowStyle CssClass="mGridAlternate"></AlternatingRowStyle>
        <EmptyDataTemplate>No vendors found</EmptyDataTemplate>
        <PagerTemplate>
                <table cellspacing="8px" width="100%">
                    <tr>
                        <td align="left" style="text-align:left">
                            <asp:DropDownList ID="ddl_rowsPerPage" runat="server" AutoPostBack="true"
                                OnSelectedIndexChanged="ddl_rowsPerPage_SelectedIndexChanged"></asp:DropDownList>
                        </td>

                        <td style="text-align:right">
                            <%=gvVendor.PageIndex + 1%> of <%=gvVendor.PageCount%>
                            <asp:DropDownList ID="ddl_Paging" runat="server" AutoPostBack="true" 
                                OnSelectedIndexChanged="ddl_Paging_SelectedIndexChanged"></asp:DropDownList>
                        </td>
                        <td>

                        </td>
                        <td style="text-align:right">
                            <%=CType(Session("VendorGridList"), System.Data.DataView).Count%> rows returned
                        </td>
                     </tr>
                </table>
         </PagerTemplate>
    <Columns>
        some columns
    <Columns>
</asp:GridView>

The code where I face the error is in the databound function of the gridview

Protected Sub gvVendor_DataBound(ByVal sender As Object, ByVal e As EventArgs)
        'Dropdownlist for page number

        Dim ddl_paging As DropDownList = CType(gvVendor.BottomPagerRow.FindControl("ddl_Paging"), DropDownList)

        For count As Integer = 0 To gvVendor.PageCount - 1
            Dim curr As Integer = count + 1
            Dim item As New ListItem(curr.ToString(), curr.ToString())

            If count = gvVendor.PageIndex Then
                item.Selected = True
            End If

            ddl_paging.Items.Add(item)
        Next

        'Dropdownlist for pagesize
        Dim ddl_row As DropDownList = CType(gvVendor.BottomPagerRow.FindControl("ddl_rowsPerPage"), DropDownList)

        ddl_row.Items.Add(New ListItem("All", "all"))
        For count As Integer = 10 To CType(gvVendor.DataSource, DataView).Count
            Dim item As New ListItem(count.ToString(), count.ToString())

            If count = gvVendor.PageSize Then
                item.Selected = True
            End If

            ddl_row.Items.Add(item)
            count += 9
        Next

    End Sub

Initially I was of the opinion that the I get this when I bind it with an empty dataview. So I simulated this scenario and I did face the issue once but on the second run the application ran successfully.

Please let me know if you can find the issue.

Setsuna F. Seiei
  • 192
  • 1
  • 2
  • 13
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Apr 02 '14 at 13:59
  • @JohnSaunders Thanks for the link. I understand your frustration when beginners like me ask the same question again and again. But I hope you do understand that the question I have posted is to understand why I get object not found in this specific case. I understand the error means the object was not found, but my query is to understand why is the application not able to find the dropdownlist when the pager row clearly has a dropdownlist. Is this specific to the way gridview binds data? Please let me know if you want me to rephrase my question so that it is more informative. – Setsuna F. Seiei Apr 02 '14 at 14:13
  • I actually want beginners like you to _think_. If the control was not found (and, _where_ was it being searched for? In what _collection_?), then maybe it wasn't there. Go use the debugger and find out what was in the collection you were searching and what was _not_ there. – John Saunders Apr 02 '14 at 14:16
  • I also suggest you get rid of that commented-out markup, or at least don't post it here - it distracts from your actual question. – John Saunders Apr 02 '14 at 14:17
  • @John good question and answer you have there. Coder, please set a breakpoint, so you can let us know which line of gvVendor_DataBound generates the error? – Jimmy Smith Apr 02 '14 at 16:04
  • Sorry I missed to mention the line of error. I get the error on the line where I use the FindControl function (i.e. first line of gvVendor_DataBound). It is hard to debug as there is no collection as such where I can go and check if the dropdownlist exist. As you can see I have declared the dropdown in the pagertemplate section of the html. I am aware that these controls will only be initialized when grdiview is binded to data. Hence I tried to fetch the control in the databound method. But I am sure I am missing something. – Setsuna F. Seiei Apr 02 '14 at 18:16

0 Answers0