0

When i run my program i get the error "Object refferance not set to an instance of object" im not sure what this is reffering to or how to fix this. i am trying to calculate the sum of all values within a colum of my database, Any help would be great.

            DataTable table = ds.Tables["Finance"];   
        //Error Here         
        int sumIncome = Convert.ToInt32(table.Compute("SUM(Income)", string.Empty));
        int sumExpenditure = Convert.ToInt32(table.Compute("SUM(Expenditure)", string.Empty));

        int calc;
        calc = sumIncome - sumExpenditure;
        string ProfitLoss = $"{calc:C2}";
        lblProfitLoss.Text = ProfitLoss.ToString();
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • Looks like first of all you need to check resut of table.Compute for dbnull. Put result to some var and look what Compute method return. – Ivan Khorin Dec 08 '20 at 15:56
  • 2
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – ADyson Dec 08 '20 at 15:56

0 Answers0