By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,417 Members | 904 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,417 IT Pros & Developers. It's quick & easy.

GridView ChechBox check all in vb.net

P: 1
I am getting error as Object reference not set to an instance of an object.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 251: ' .FindControl("chk"), CheckBox)
Line 252: If chk IsNot Nothing Then
Line 253: chk.Checked = arr.Contains(gvAll.DataKeys(i).Value)
Line 254:
Line 255: If Not chk.Checked Then
please help me


Expand|Select|Wrap|Line Numbers
  1.  Private Sub GetData()
  2.         Dim arr As ArrayList
  3.         If ViewState("SelectedRecords") IsNot Nothing Then
  4.             arr = DirectCast(ViewState("SelectedRecords"), ArrayList)
  5.         Else
  6.             arr = New ArrayList()
  7.         End If
  8.         Dim chkAll As CheckBox = DirectCast(gvAll.HeaderRow _
  9.                     .Cells(0).FindControl("chkAll"), CheckBox)
  10.         For i As Integer = 0 To gvAll.Rows.Count - 1
  11.             If chkAll.Checked Then
  12.                 If Not arr.Contains(gvAll.DataKeys(i).Value) Then
  13.                     arr.Add(gvAll.DataKeys(i).Value)
  14.                 End If
  15.             Else
  16.                 Dim chk As CheckBox = DirectCast(gvAll.Rows(i).Cells(0) _
  17.                                             .FindControl("chk"), CheckBox)
  18.                 If chk.Checked Then
  19.                     If Not arr.Contains(gvAll.DataKeys(i).Value) Then
  20.                         arr.Add(gvAll.DataKeys(i).Value)
  21.                     End If
  22.                 Else
  23.                     If arr.Contains(gvAll.DataKeys(i).Value) Then
  24.                         arr.Remove(gvAll.DataKeys(i).Value)
  25.                     End If
  26.                 End If
  27.             End If
  28.         Next
  29.         ViewState("SelectedRecords") = arr
  30.     End Sub
  31.  
  32.     Private Sub SetData()
  33.         Dim i As Integer
  34.         Dim var = DirectCast(ViewState("SelectedRecords") _
  35.                                         , ArrayList)
  36.         ' Dim s = DirectCast(ViewState("SelectedRecords") _
  37.         '   , ArrayList)
  38.         Dim currentCount As Integer = 0
  39.         Dim chkAll As CheckBox = DirectCast(gvAll.HeaderRow _
  40.                         .Cells(0).FindControl("chkAll"), CheckBox)
  41.         chkAll.Checked = True
  42.         Dim arr As ArrayList = DirectCast(ViewState("SelectedRecords") _
  43.                                         , ArrayList)
  44.  
  45.         For i = 0 To gvAll.Rows.Count - 1
  46.  
  47.             Dim chk As CheckBox = DirectCast(gvAll.Rows(i).Cells(0) _
  48.                                             .FindControl("chk"), CheckBox)
  49.             'Dim var = DirectCast(gvAll.Rows(i).Cells(0) _
  50.             ' .FindControl("chk"), CheckBox)
  51.             If chk IsNot Nothing Then
  52.                 chk.Checked = arr.Contains(gvAll.DataKeys(i).Value)
  53.  
  54.                 If Not chk.Checked Then
  55.                     chkAll.Checked = False
  56.                 Else
  57.                     currentCount += 1
  58.                 End If
  59.             End If
  60.         Next
  61.         hfCount.Value = (arr.Count - currentCount).ToString()
  62.     End Sub
Oct 29 '13 #1
Share this Question
Share on Google+
1 Reply


Frinavale
Expert Mod 5K+
P: 9,731
Could be that the Cells property is nothing... Try using the FindControl method on the row instead:

Expand|Select|Wrap|Line Numbers
  1.   Dim chk As CheckBox = DirectCast(gvAll.Rows(i).FindControl("chk"), CheckBox)
  2.  
  3.   If chk IsNot Nothing Then
  4.     chk.Checked = arr.Contains(gvAll.DataKeys(i).Value)
  5.     If Not chk.Checked Then
  6.       chkAll.Checked = False
  7.     Else
  8.       currentCount += 1
  9.     End If
  10.   End If
-Frinny
Oct 29 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.