troubleshooting Question

SUM of column in datatable

Avatar of DaveMon
DaveMonFlag for United States of America asked on
C#ASP.NETSQL
3 Comments1 Solution3679 ViewsLast Modified:
Hello Experts,  I have a question about a web project I am working on, particularly, I am trying so get the SUM of a column of my databound Gridview. It currently works fine and displays three columns, but i am having problems tallying the last column for a total.



   protected void btnStart_Click(object sender, EventArgs e)
    {
   
string sql = string.Format("SELECT DISTINCT OrdFile.Order#, OrdFile.Quote#, (QtFile.TotBoxSqFtWalls + QtFile.TotBoxSqFtTops + QtFile.TotBoxSqFtFlrs) FROM OrdFile INNER JOIN QtUnit ON (OrdFile.Quote# = QtUnit.Quote#) INNER JOIN QtFile ON (QtFile.Quote# = OrdFile.Quote#) AND (QtFile.QtBox# = OrdFile.QtBox#) WHERE OrdFile.DateSchProd BETWEEN {0} AND {1} AND OrdFile.Quote# = QtUnit.Quote#", startDate.Value, endDate.Value);
       
        DataSet ds1 = new DataSet();
        kpweb.DBService mydb = new kpweb.DBService();
        ds1 = mydb.runQTSQL(sql);

        GridView1.DataSource = ds1.Tables[0].DefaultView;
        GridView1.DataBind();


       if (ds1.Tables[0].Rows[0][0].ToString().ToUpper() != "NOTHING")
        {
            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
          string sqlJobStatusHistory = string.Format("SELECT SUM(Column1) FROM ds1.Tables[0]", ds1.Tables[0].Rows[i][1].ToString(), ds1.Tables[0].Rows[i][2].ToString());

                using (SqlConnection con = new SqlConnection(constrSQL))
                {
                    con.Open();
                    using (SqlCommand cmd = new SqlCommand(sqlJobStatusHistory, con))
                    {
                        try
                        {
                            sqlJobStatusHistory = cmd.ExecuteScalar().ToString();

                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.Footer)
        {

            e.Row.Cells[1].Text = string.Format("Total :");
        
         //  Heres where I want the total displayed
         // e.Row.Cells[2].Text = string.Format("Total :");

        }
    }

thanks for any help on this.

Dave
ASKER CERTIFIED SOLUTION
DaveMon

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros