Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
I am using following code for sum of grid view column.
Dim a, b, c As Decimal
        For i = 0 To GridItem.Rows.Count
            a = Convert.ToDecimal(GridItem.Rows(i).Cells(7).Value.ToString())
            c = c + a
        Next
        
        txtBasic.Text = c
System throws an error:Object reference not set to an instance of an object.
after debug the code I understand the problem.I add 2 rows in grid view.
Problem is system shows Gridview1.Rows.Count=3.
as per count for loop iterate 3 times 3rd time system throws an error:Object reference not set to an instance of an object.
 
1st time I entered =500.2nd time=2000.at second iteration of for loop value of c=2500.
Posted 9-Jan-13 3:12am
Edited 9-Jan-13 3:16am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Whenever you work with an object hierarchy, you should always perform an existence check before attempting to drill down. That way you eliminate issues like what you are encountering here.
For example.
Dim a, b, c As Decimal
For i = 0 to GridItem.Rows.Count
If !IsNothing(GridItem.Rows(i)) And !IsNothing(GridItem.Rows(i).Cells(7) Then
   If Decimal.TryParse( GridItem.Rows(i).Cells(7).Value, out a ) Then
      c = c + a
   End If
End If
txtBasic.Text = c.ToString()
  Permalink  
v2
Comments
Sandeep Mewara at 9-Jan-13 9:38am
   
5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Entire solution is given over here regarding your problem :
http://csharpdotnetfreak.blogspot.com/2009/07/display-total-in-gridview-footer.html[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

For i = 0 To GridItem.Rows.Count - 1
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 575
1 George Jonsson 285
2 Garth J Lancaster 280
3 Sergey Alexandrovich Kryukov 264
4 Maciej Los 250
0 Sergey Alexandrovich Kryukov 10,879
1 OriginalGriff 5,555
2 Maciej Los 5,534
3 CPallini 3,819
4 BillWoodruff 3,594


Advertise | Privacy | Mobile
Web04 | 2.8.141030.1 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100