473,394 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

GridView ChechBox check all in vb.net

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
1 3632
Frinavale
9,735 Expert Mod 8TB
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

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

Similar topics

2
by: Chris Davoli | last post by:
How do you enable a check box in the GridView. I selected Checkbox Field in the Columns of the GridView, and the check box shows up in the Grid view, but it is disabled. How do I enable it so I can...
1
by: chandrasekhar | last post by:
i have two pages is my webform , one page with gridview with checkboxes , i want to select checkboxes and click on submit button , then control passes to another page where i have one more gridview...
0
by: tbirg | last post by:
Hi All, I have a webpage which uses an access DB. I used an access data source and a gridview in the page. The gridview allows editing of the rows. I have set some of the columns to readonly....
0
by: anupsonawane | last post by:
hi friends i want to add chechbox control in datagrid at runtime in vb.net 2005 so plz send me useful inf abt how to add this control at runtime & how to get check event on this checkbox....
3
by: Shaileshsharma | last post by:
Hi, I am using Grid View Template field(Check Box) for delete a row, but i need a message box(OK/Cancle) should display before deleting the row.My C# Code like this for deleting the row when...
0
by: shona | last post by:
Hi, i want to exeute one function & one javascript on checking a chechbox in gridview control but my problem is that i want to use the values which i get from function & after that i want to run...
2
by: tshad | last post by:
I have have a template column in my GridView with a textbox that accepts a dollar amount and I want to check to see if the value is numeric as the person exits the field? Can I use a Validator...
1
by: Vajrala Narendra | last post by:
Hi, in .net to make gridviews header fixed i wrote a function in master page but it is working for few gridviews only, and few gridviews not displaying fixedheader, header size increasing according...
2
by: sana krishna | last post by:
i use ASP.NET 2005 I use gridview to display the Employ information.And display a checkbox filed inside the gridview add as a template. I want to store the emp_no into a variable when i checked...
2
by: Cirene | last post by:
I have a simple, 3 column, databound gridview that is laid out like this... QTY (textbox for input), PRODUCT, PRICING The user can input a quantity for 1 or more items in the gridview. When...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.