locked
sum colume and display in lable RRS feed

  • Question

  • User-775536502 posted

    hii 

    i have database with some columes 

    1) name

    2) date

    3) total 

    i want to sum all the columes of total and display in lable 

    how can i do it ?

    Tuesday, May 8, 2018 10:25 AM

Answers

  • User-369506445 posted

    hi

    please try below code :

    Code behind

    protected void Page_Load(object sender, EventArgs e)
            {
                string connectionString = @"Data Source=PC-BAKHTIARI\SQLEXPRESS;Initial Catalog=YourDataBaseName;Integrated Security=True;";
                string selectTopics = "select sum(total) from yourtablename";
                // Define the ADO.NET Objects
                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    SqlCommand topiccmd = new SqlCommand(selectTopics, con);
                    con.Open();
                    int numrows = (int)topiccmd.ExecuteScalar();
                    if (numrows == 0)
                    {
                        labal1.Text = numrows.ToString();
                    }
                }
    
            }     

    in Html

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
         
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
               <asp:Label ID="labal1" runat="server"/>
            </div>
        </form>
    </body>
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 8, 2018 12:56 PM