locked
Calculate grand total from Dataset RRS feed

  • Question

  • User340377605 posted

    Hello,

    I want to get (total)  column from DB into Dataset,, and the calculate the grand total , and show result into Textbox !!

    This what i am doing to get total into Dataset: what should i do next to calculate the Grand total ?? and show it in TextBox ??


    DataSet ds = new DataSet();
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString))
            {
                con.Open();
                SqlDataAdapter cmd = new SqlDataAdapter("Select total FROM books WHERE quid = '" + (Label1.Text) + "'", con);
                cmd.Fill(ds, "table");
            }


    Saturday, February 5, 2011 5:56 PM

Answers

  • User1224194097 posted

    Retrieve Total from the DataTable like this

    TextBox1.Text=ds.Tables[0].Rows[0]["total"].ToString();

    Modify the command to return 0 for NULL values.

     SqlDataAdapter cmd = new SqlDataAdapter("Select ISNULL(total,0) total FROM books WHERE quid = '" + (Label1.Text) + "'", con); 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 5, 2011 6:27 PM
  • User1224194097 posted

    If you want to get grand total, you can get it in the query itself. No need to loop through DataTable to add totals.

    SqlDataAdapter cmd = new SqlDataAdapter("Select ISNULL(SUM(total),0) total FROM books WHERE quid = '"+(Label1.Text)+"'", con);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 5, 2011 7:35 PM

All replies

  • User1224194097 posted

    Retrieve Total from the DataTable like this

    TextBox1.Text=ds.Tables[0].Rows[0]["total"].ToString();

    Modify the command to return 0 for NULL values.

     SqlDataAdapter cmd = new SqlDataAdapter("Select ISNULL(total,0) total FROM books WHERE quid = '" + (Label1.Text) + "'", con); 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 5, 2011 6:27 PM
  • User340377605 posted

    I am not just trying to retrieve total ,, I am trying to get the Grand Total which is all total added together

    i.e. total1 + total2+ total3  + ....etc = Grand Total

    total1 , total2, total3 ... etc are the Dataset I am getting from DB ,, How can i add them ??

    Saturday, February 5, 2011 7:05 PM
  • User1224194097 posted

    If you want to get grand total, you can get it in the query itself. No need to loop through DataTable to add totals.

    SqlDataAdapter cmd = new SqlDataAdapter("Select ISNULL(SUM(total),0) total FROM books WHERE quid = '"+(Label1.Text)+"'", con);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 5, 2011 7:35 PM
  • User-691245060 posted

    Alternatively if you want to do it on the DataTable itself...then go for LINQ - 

    http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/b5184aca-6264-4079-8434-7a0cf43baf2c

    Thanks.

    Sunday, February 6, 2011 12:39 AM
  • User340377605 posted

    If you want to get grand total, you can get it in the query itself. No need to loop through DataTable to add totals.

    1. SqlDataAdapter cmd = new SqlDataAdapter("Select ISNULL(SUM(total),0) total FROM books WHERE quid = '"+(Label1.Text)+"'", con);  
    SqlDataAdapter cmd = new SqlDataAdapter("Select ISNULL(SUM(total),0) total FROM books WHERE quid = '"+(Label1.Text)+"'", con);

    Thank you ,, can i show the grand total in a textBox like this:

    cmd.Fill(ds, "table");
              TextBox1.Text = ds.ToString();

    Sunday, February 6, 2011 2:38 AM
  • User-1485051499 posted

    To show the grand total use

    TextBox1.Text = ds.Tables[0].Rows[0]["total"].ToString();


    Sunday, February 6, 2011 2:53 AM
  • User340377605 posted

    Thank you so much ,, it is working fine :D



    Sunday, February 6, 2011 12:11 PM