Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working with an application and need to calculate the sum off all values of a column in data grid, just to show the user how much amount he spent on the job code when he selects it.
I called a data set and view all the data items and tried to add it but exception keeps on coming I tried all the ways I know its simple but not working

My field to be added is Long integer in database and I fill the table using dataset
and after that I called the add function.

Please can anyone help. Don't avoid hurting my feelings simple because I am a beginner

My code
C#
public DriverPaymentForm(DriverPaymentBean driverpaymentbean)
        {
            InitializeComponent();

            fillForm(driverpaymentbean);
            showallPaymentbyjobcode();
            addtotalpaid();

        }

public void showallPaymentbyjobcode()
        {
            try
            {



                String query = "Select jobcode ,vehicleno,amount,postdate,description,ischeque,iscash from driverpaymentmastertable  where jobcode Like '" + txtjobCode.Text + "'  Order by postdate";
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
                DataSet ds = new DataSet();
                dAdapter.Fill(ds, "tblpayview");
                tblpaymentview.DataSource = ds.Tables["tblpayview"].DefaultView;
                if (ds.Tables.Count <= 0)
                {
                    lblstatus.Text = "No Payment Details Present";
                }
                
            }
            catch (Exception)
            {
                MessageBox.Show("The application had met with some errors please restart  the application :\n error:closer MSAccess files");

            }


        }

        public void addtotalpaid()
        {
           long  sum = 0;
            int count = tblpaymentview.RowCount;
            if (count != null&count!=0)
            {

                for (int i = 0; i < count; i++)
                {

                    sum = sum + long.Parse(tblpaymentview.Rows[i].Cells[3].Value.ToString());

                }
            }

        }


Exception null reference exception at addition line
Posted
Updated 9-Oct-11 6:23am
v2

1 solution

If this is your actual code start by replacing
C#
if (count != null&count!=0)
with
C#
if (count!=0)


Next add a check to see if the cell value is not null
C#
if( tblpaymentview.Rows[i].Cells[3].Value != null )
{
  sum = sum + long.Parse(tblpaymentview.Rows[i].Cells[3].Value.ToString());
}


As your code uses the term payment you may want to use double instead of long as monetary amounts usually have digits.
 
Share this answer
 
v3
Comments
SREENATH GANGA 9-Oct-11 13:17pm    
THANK YOU A LOT
André Kraak 9-Oct-11 14:33pm    
Your welcome.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900