14,301,556 members
Rate this:
See more:
```protected void btnmakepmnt_click(object sender, EventArgs e)
{
DataTable dtpymnt = (DataTable)Session["checkdata"];

if (rdio_rps.Checked == true)
{

var sum = dtpymnt.AsEnumerable().Sum(dr => dr.Field<int>("SevicePriceRup"));
Label2.Text = sum.ToString();
}
}

// here datatable return 3 column with records, i want to sum values of column "SevicePriceRup", please check the code or provide me other ways to resolve.
here "sevicePriceRup" has datatype bigint```
Posted
Updated 15-Jan-16 20:24pm
v6
Member 12175752 16-Jan-16 2:04am

here "sevicePriceRup" has datatype bigint

Rate this:

## Solution 2

With big integer, one way is to calculate the sum by hand. It's not 'pretty' but it works. Consider the following example:
```System.Data.DataTable table = new System.Data.DataTable();

System.Numerics.BigInteger sum = 0;
foreach (System.Data.DataRow dr in table.Rows)
{
}```

If you need the calculation in different places, you can create for example an extension method to ease the coding.
```public static class BI
{
public static System.Numerics.BigInteger SumBigIntCol(this System.Data.DataTable table, string columnName)
{
System.Numerics.BigInteger sum = 0;
foreach (System.Data.DataRow dr in table.Rows)
{
}

return sum;
}
}```

And to use would look like
`System.Numerics.BigInteger calcresult = table.SumBigIntCol("Col1");`
Richard Deeming 16-Jan-16 6:25am

NB: SQL's `bigint` maps to .NET's `Int64` type, not `BigInteger`. AFAIK, SQL doesn't have an equivalent for `BigInteger`.
Wendelius 16-Jan-16 6:29am

That's a good point. If the data comes from SQL Server and from a bigint column then Int64 would be the correct choice.
Rate this:

## Solution 3

One way to approach this is to define extension methods:
```public static class DataTableExtensions
{
public static int SumColumnIntByIndex(this DataTable table, int id)
{
return table.AsEnumerable().Sum(x => x.Field<int>(id));
}

public static decimal SumColumnDecByIndex(this DataTable table, int id)
{
return table.AsEnumerable().Sum(x => x.Field<decimal>(id));
}

public static int SumColumnIntByID(this DataTable table, string id)
{
return table.AsEnumerable().Sum(x => x.Field<int>(id));
}

public static decimal SumColumnDecByID(this DataTable table, string id)
{
return table.AsEnumerable().Sum(x => x.Field<decimal>(id));
}
}```
Sample use: assuming you have a valid DataTable, "dt," with a Decimal Field at Column Index #3, and Column #3 is named "Salary:"
```decimal total1 = dt.SumColumnDecByIndex(3);
decimal total2 = dt.SumColumnDecByID("Salary");```
You can easily add similar extensions for other Types (long, double, etc.). However note that as of .NET 5.0 IEnumerable.Sum can be used with only these Types:
```double,double?
int,int?
decimal,decimal?
long,long?
float,float?```
While you could do some re-factoring and make an extension that handles multiple Types, I personally believe that in writing Extensions the "DRY" principle ("don't repeat yourself") should be ignored, and that it's better to write separate Extensions ... which also gets you out of the problem of how to return the right Type you'll have with a generic Extension.
v6
Rate this:

## Solution 1

Try the below code

```protected void btnmakepmnt_click(object sender, EventArgs e)
{
DataTable dtpymnt = (DataTable)Session["checkdata"];
if (rdio_rps.Checked == true)
{
object sumObject;
sumObject = dtpymnt.Compute("Sum(SevicePriceRup)", "");
Label2.Text = sumObject .ToString();
}
}```
Member 12175752 16-Jan-16 2:34am

Hi, salman
Now m getting error "Invaild usage aggregate function sum() and Type String.
at
sumObject = dtpymnt.Compute("Sum(SevicePriceRup)", "");// this line
Salman622 21-Jan-16 8:14am

try out
SumObject = dtpymnt.Compute("Sum(SevicePriceRup)", "[SevicePriceRup] IS NOT NULL");
Rate this:

## Solution 4

SQL's `bigint` type maps to .NET's `Int64` type. You're trying to cast the column to `Int32` to compute the sum, which won't work.

Change the code to sum the column using the correct type:
`var sum = dtpymnt.AsEnumerable().Sum(dr => dr.Field<long>("SevicePriceRup"));`