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.