locked
Display Sum of column in TextBox RRS feed

  • Question

  • User1920384055 posted

    Hi guys

    Could anyone please help me out, I am trying to display the sum of a column in an sql table in a Text Box, I am really stuck on this and appreciate any help please

    So I have 20 columns and I want to display the sum of the each column (RB1 to RB20) in textboxes (TextBox1 to TextBox20)

    Could anyone please help on this? 

    Should this be done as a stored procedure or within the code behin and how?

    Kind regards

    Sunday, August 31, 2014 10:36 AM

Answers

  • User2103319870 posted

    Are you looking for a source code to get the count of a column from database and display it in textbox. if that is the case then you can use the below code

    //Variable to hold the value
                int TotlaValues = 0;
                //Setting the connection string
                SqlConnection conn = new SqlConnection(@"Data Source=YourDBServer;Initial Catalog=NORTHWIND;User ID=YourUserID;password=yourPassword;");
                //Opening the connection
                conn.Open();
                //Setting the command
                SqlCommand sc = new SqlCommand("SELECT SUM(o.EmployeeID) AS TotalCount FROM dbo.Orders o", conn);
                //Creating object of reader
                SqlDataReader reader;
                //Executing the reader
                reader = sc.ExecuteReader();
                while (reader.Read())
                {
                    //Get the Sum of Column from Database
                   txtTotalCount.Text =  reader["TotalCount"].ToString();
                }
                conn.Close();

    Above example fetch the details from Northwind database and you need to change this as per your database.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 31, 2014 11:12 AM

All replies

  • User-1716253493 posted
    TextBox1.Text=rdr.GetInt32(0).ToString();
    Sunday, August 31, 2014 10:48 AM
  • User1920384055 posted

    Could you please expand on that? Not sure how to use this

    Best regards

    Sunday, August 31, 2014 10:52 AM
  • User2103319870 posted

    Are you looking for a source code to get the count of a column from database and display it in textbox. if that is the case then you can use the below code

    //Variable to hold the value
                int TotlaValues = 0;
                //Setting the connection string
                SqlConnection conn = new SqlConnection(@"Data Source=YourDBServer;Initial Catalog=NORTHWIND;User ID=YourUserID;password=yourPassword;");
                //Opening the connection
                conn.Open();
                //Setting the command
                SqlCommand sc = new SqlCommand("SELECT SUM(o.EmployeeID) AS TotalCount FROM dbo.Orders o", conn);
                //Creating object of reader
                SqlDataReader reader;
                //Executing the reader
                reader = sc.ExecuteReader();
                while (reader.Read())
                {
                    //Get the Sum of Column from Database
                   txtTotalCount.Text =  reader["TotalCount"].ToString();
                }
                conn.Close();

    Above example fetch the details from Northwind database and you need to change this as per your database.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 31, 2014 11:12 AM
  • User-1806150748 posted

    If there was SP call to fill initial 20 columns, then we should have a DB call to get those 20 column values, and at that time, we could have make SUM of those 20 columns, instead of one more DB call to get SUM, this just a suggestion over what you proposed.

    Sunday, August 31, 2014 1:13 PM