?
Solved

Sum of column in datagrid

Posted on 2006-06-11
5
Medium Priority
?
650 Views
Last Modified: 2008-03-17
Hi, I want tot find the sum of a colum in a datagrid. Below is the code that I am using but the problemm is that if there is no data in the table, it does not work. I get the error message: Cast from type 'DBNull' to type 'Decimal' is not valid.


TotalSoldPrice = objDS.Tables(0).Compute("SUM(SoldBid)", String.Empty).ToString
txtTotalSoldPrice.Text = FormatCurrency(TotalSoldPrice)

Can you tell me how I can get around this. I want to use this code because it is the simplest one that I have found thus far.
Thank you.

P.S, I am using SQL database, SQL Server 2003 and APS.NET and VB.NET
0
Comment
Question by:Dorneika
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 5

Accepted Solution

by:
t_itanium earned 252 total points
ID: 16883272
hi
instead of this....
TotalSoldPrice = objDS.Tables(0).Compute("SUM(SoldBid)", String.Empty).ToString
txtTotalSoldPrice.Text = FormatCurrency(TotalSoldPrice)

try to iterate ove all rows in the datagrid...  and check for the values..if it is not null...add them to avariable thenn display this variable....


algorithm:

for (each row in datagrid rows)
if (cell is not null)
variable+=cell.value


display variable

cheers
0
 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 248 total points
ID: 16883562
Or if you want to do it from the datatable itself, you can add an expression column, and compute on that.  On these lines

        Dim dc As New DataColumn
        dc.Expression = "ISNULL(SoldBid,0)"
        dc.DataType = GetType(Decimal)
        dc.ColumnName = "SoldBidWithoutNull"
        objDS.Tables(0).Columns.Add(dc)
        TotalSoldPrice = objDS.Tables(0).Compute("SUM(SoldBidWithoutNull)", String.Empty).ToString

The creation of the new column would only need doing once, not every time you wanted to calculate/re-calculate the TotalSoldPrice.

Roger
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question