locked
How do i SUM two numbers in two columns in Database RRS feed

  • Question

  • User-2074858223 posted

    I want to sum these two columns but am getting this error

    error

    Server Error in '/' Application.
    Incorrect syntax near the keyword 'AND'.
    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.
    
    Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'AND'.
    
    Source Error:
    
    
    Line 30:             cmd.Parameters.AddWithValue("@TransactionDate", DateTime.Today);
    Line 31:             SqlDataAdapter da = new SqlDataAdapter(cmd);
    Line 32:             da.Fill(dt);
    Line 33:             con.Close();
    Line 34:             getdailytotal.DataSource = dt;
      protected void BindExpenses()
        {
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(str))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select SUM (TotalAmount) TotalAmount, (Liters) Liters from Sells  WHERE TransactionDate=@TransactionDate", con);
                cmd.Parameters.AddWithValue("@TransactionDate", DateTime.Today);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                con.Close();
                getdailytotal.DataSource = dt;
                getdailytotal.DataBind();
    Thursday, November 30, 2017 7:44 PM

Answers

  • User-707554951 posted

    Hi micah2012,

    You sql query statement seems have something wrong.

    You should have something as below:

    Code as below:

      <asp:GridView ID="getdailytotal" runat="server"></asp:GridView>

    CodeBehind:

    using System.Configuration;
    using System.Data.SqlClient;
    using System.Data;
     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    this.Getnotify();
                  
                }
            }
           
            private void Getnotify()
            {
                string constring4 = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constring4))
                {
                    using (SqlCommand cmd = new SqlCommand("select SUM (TotalAmount) TotalAmount, SUM(Liters) Liters from Sells  WHERE TransactionDate=@TransactionDate", con))
                    {
                        cmd.Parameters.AddWithValue("@TransactionDate", DateTime.Today);
                        DataTable dt = new DataTable();
                        SqlDataAdapter sda = new SqlDataAdapter(cmd);
                        sda.Fill(dt);
                        getdailytotal.DataSource = dt;
                        getdailytotal.DataBind();
                    }
                }
            }

    Output as below:

    Best regards 

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 1, 2017 8:54 AM

All replies

  • User475983607 posted

    The error states there is an error near "AND" but the SQL you've posted does not have the word "AND" within the script.  I assume, once again, you are not showing us the actual code.

    As for summing two columns, if the column are numeric just use the addition symbol. 

    For example...

    IF OBJECT_ID('tempdb..#Sells') IS NOT NULL
    	DROP TABLE #Sells
    
    
    CREATE TABLE #Sells (
    	Num1	INT,
    	Num2	INT,
    )
    
    INSERT INTO #Sells(Num1, Num2)
    VALUES (1,2)
    
    SELECT *
    FROM #Sells
    
    SELECT (Num1 + Num2) AS [Total]
    FROM #Sells
    
    Thursday, November 30, 2017 7:53 PM
  • User-707554951 posted

    Hi micah2012,

    You sql query statement seems have something wrong.

    You should have something as below:

    Code as below:

      <asp:GridView ID="getdailytotal" runat="server"></asp:GridView>

    CodeBehind:

    using System.Configuration;
    using System.Data.SqlClient;
    using System.Data;
     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    this.Getnotify();
                  
                }
            }
           
            private void Getnotify()
            {
                string constring4 = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constring4))
                {
                    using (SqlCommand cmd = new SqlCommand("select SUM (TotalAmount) TotalAmount, SUM(Liters) Liters from Sells  WHERE TransactionDate=@TransactionDate", con))
                    {
                        cmd.Parameters.AddWithValue("@TransactionDate", DateTime.Today);
                        DataTable dt = new DataTable();
                        SqlDataAdapter sda = new SqlDataAdapter(cmd);
                        sda.Fill(dt);
                        getdailytotal.DataSource = dt;
                        getdailytotal.DataBind();
                    }
                }
            }

    Output as below:

    Best regards 

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 1, 2017 8:54 AM