SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=Demo_eLogsVA4;Integrated Security=True");
//Write a select Query
cn.Open();
string q = "Select Invoice_Code,Amount,Tax,Net Where Invoice_Code='INV991'";
DataSet dset = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(q, cn);
DataTable dtable = new DataTable();
da.Fill(dtable);
//Bind grid.
GridView1.DataSource = dtable;
DataRow[] dr = dtable.Select("Sum(Net)");
GridView1.FooterRow.Cells[0].Text = Convert.ToString(dr[0]);
GridView1.DataBind();
cn.Close();
the above is my gridbind method. My table name is dtable, from that dtable i want to sum the amount and net field in the datatable(dtable) and where i want to include the sum code ,Help please
the above is my gridbind method. My table name is dtable, from that dtable i want to sum the amount and net field in the datatable(dtable) and where i want to include the sum code ,Help please
Jean PaulPosted Nov 5, 2011, 5:32 AM
You can use the code soon after you fill the DataTable.
(I believe your Net column is a numeric type - Also the table name is missing in the query)
object obj = dtable.Compute("SUM(Net)", string.Empty);
double value = (double)obj;
Suthish NairPosted Nov 6, 2011, 12:59 PM
Prabhu RajaPosted Nov 5, 2011, 8:09 AM
Rajkumar RPosted Nov 5, 2011, 7:59 AM
Prabhu RajaPosted Nov 5, 2011, 7:40 AM
Rajkumar RPosted Nov 5, 2011, 7:31 AM
Now i m getting the following error
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Cannot perform '>' operation on System.String and System.Int32.
Exception Details: System.Data.EvaluateException: Cannot perform '>' operation on System.String and System.Int32.
Source Error:
Line 35: //Bind grid. Line 36: GridView1.DataSource = dtable; Line 37: object obj = dtable.Compute("Sum(Net)", "Net>0"); Line 38: decimal value; Line 39: if (obj != null)Prabhu RajaPosted Nov 5, 2011, 7:21 AM
You may Try this,
Decimal value;
if ( obj != null)
{
value = (Decimal) obj;
}
{
// obj is null.
}
Rajkumar RPosted Nov 5, 2011, 6:42 AM
Rajkumar RPosted Nov 5, 2011, 6:08 AM
once i added the above code i m getting the following error
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Invalid usage of aggregate function Sum() and Type: String.
Exception Details: System.Data.DataException: Invalid usage of aggregate function Sum() and Type: String.
Source Error:
Source File: d:\WebSite1\Default10.aspx.cs Line: 36
This is my full code
Rajkumar RPosted Nov 5, 2011, 6:03 AM
and i need to show that total in the footer row in the gridview
Here U missing the bind function
thanks in advance
Rajkumar RPosted Nov 5, 2011, 5:11 AM
Invalid usage of aggregate function Sum() and Type: String.
Jean PaulPosted Nov 5, 2011, 5:09 AM
object obj = dtable.Compute("SUM(Net)", string.Empty);
double value = (double)obj;