GridView1I am trying to switch my code on gridview from this:
<asp:SqlDataSource ID="dsPar" runat="server" ConnectionString="<%$ ConnectionStrings:connstring %>"
SelectCommand="SELECT ID, FileNumber, address, phone from myTable ORDER BY ID" FilterExpression="ID like '%{0}%'">
<FilterParameters>
<asp:ControlParameter Name="StreetSrch" ControlID="searchBox" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
to this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strQuery As String = "SELECT ID, FileNumber, address, Phone from MyTable WHERE Id LIKE '%@strSearch%' ORDER BY Id"
Dim cmd As New SqlCommand(strQuery)
Dim dt As DataTable = GetData(cmd)
Dim CheckBoxArray As ArrayList
If ViewState("CheckBoxArray") IsNot Nothing Then
CheckBoxArray = DirectCast(ViewState("CheckBoxArray"), ArrayList)
Else
CheckBoxArray = New ArrayList()
End If
If Not IsPostBack Then
Gridview1.DataBind()
Dim CheckBoxIndex As Integer
Dim CheckAllWasChecked As Boolean = False
Dim chkAll As CheckBox = DirectCast(GridView1.HeaderRow.Cells(0).FindControl("chkAll"), CheckBox)
rest of code....
End If
End Sub
Finally, below is a snapshot of the markup:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True"
OnPageIndexChanging = "OnPaging" HeaderStyle-CssClass = "header" Font-Size = "10pt"
AlternatingRowStyle-BackColor = "#C2D69B" OnRowDataBound = "RowDataBound" AllowSorting="true"
PageSize="20" CssClass="Gridview"
GridLines="None">
I am getting "Object reference not set to an instance of an object." on the following line:
Line 44: Dim chkAll As CheckBox = DirectCast(GridView1.HeaderRow.Cells(0).FindControl(“chkAll”), CheckBox)
Any idea how to resolve this?
Code is very long and I didn’t want to post all of it here. I can post more if needed.
IT is throwing a null reference exception because
doesn’t find a checkbox so it returns null (Nothing) so what you are essentially doing is trying to cast Nothing to a checkbox.