locked
using vs2008 how to sum sql database RRS feed

  • Question

  • User115673193 posted

    Dear All,

    i want to sum of column and output in a textbox. how it is possible?

     

    Monday, August 12, 2013 10:11 AM

Answers

  • User-1360095595 posted

    Change your sql to this: SELECT SUM(txtWeight) AS txtWeight FROM personalData

    But I still say using executescalar is simpler. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 12, 2013 11:53 AM

All replies

  • User-1360095595 posted

    It is possible by making use of ADO.NET. If you are not familiar with that perhaps this tutorial will get you started: 

    http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson01.aspx

    Monday, August 12, 2013 10:19 AM
  • User115673193 posted

    i m use this coding

    string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=ABJDATABASE;Integrated Security=True; Max Pool Size=100";
                string query = "SELECT SUM(txtWeight) FROM personalData";
                SqlConnection conn = new SqlConnection(connstr);
                conn.Open();
                SqlDataReader myReader = null;
                SqlCommand myCommand = new SqlCommand(query, conn);
                myReader = myCommand.ExecuteReader();

                int txtTotalWeight = 0;
                while (myReader.Read())
                {
                    txtTotalWeight += Convert.ToInt32(myReader["txtWeight"]);
                }
                conn.Close();

    Monday, August 12, 2013 10:30 AM
  • User-1360095595 posted

    Are you facing any problems or errors? One suggestion is I'd use ExecuteScalar() instead since you're returning just one value. It'll simplify your code a bit. 

    Monday, August 12, 2013 10:33 AM
  • User115673193 posted

    when run the programme

    it gives a "IndexOutofRangeException was unhandelled" error then

    "txtweight"

     

    Monday, August 12, 2013 11:23 AM
  • User-1360095595 posted

    Change your sql to this: SELECT SUM(txtWeight) AS txtWeight FROM personalData

    But I still say using executescalar is simpler. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 12, 2013 11:53 AM
  • User115673193 posted

    how to use executescalar........

    i m using this code.

    string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=ABJDATABASE;Integrated Security=True; Max Pool Size=100";
                string query = "SELECT SUM(txtWeight) AS txtWeight FROM personalData";
                SqlConnection conn = new SqlConnection(connstr);
                conn.Open();

                SqlCommand myCommand = new SqlCommand(query, conn);
                txtTotalWeight.Text = (string)myCommand.ExecuteScalar();
                conn.Close();

    it gives a error

    Unable to cast object of type 'System.Double' to type 'System.String'.

     

     

    Monday, August 12, 2013 9:24 PM
  • User-1454326058 posted

    Hi ManishArya,

    Based on the error message "Unable to cast object of type 'System.Double' to type 'System.String'", we can find the exception is due to this code "txtTotalWeight.Text = (string)myCommand.ExecuteScalar();". Please modify your code as follows:

    txtTotalWeight.Text=myCommand.ExecuteScalar().ToString();
    Or
    txtTotalWeight.Text=Convert.ToString(myCommand.ExecuteScalar());

    In conclusion:

    Use the ExecuteScalar method to retrieve a single value and the type is object. So we should force to conver the type to string.

    More information, please refer to: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.executescalar.aspx

    Thanks

    Best Regards

    Tuesday, August 13, 2013 4:09 AM