-1

i have a small problem with summarizing of the total value of the columns. I have a shopping cart where i want to summarize quantity and price of the item. I've done summarize of prices but when i was trying to multiply it to quantity it didn't work.

. Could you please give me an advice.

Here is a gridview:

<asp:GridView ID="GridView1" runat="server" HorizontalAlign="Center" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" DataKeyNames="id"  ShowFooter="true" ShowHeaderWhenEmpty="true">
    <Columns>
        <asp:BoundField DataField="item" HeaderText="Item" SortExpression="item"></asp:BoundField>
        <asp:BoundField DataField="BEE" HeaderText="Description" SortExpression="BEE"></asp:BoundField>
        <asp:BoundField DataField="count" HeaderText="Quantity" SortExpression="count"></asp:BoundField>
        <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price"></asp:BoundField>
        <asp:TemplateField>
            <FooterTemplate>
                <asp:Label ID="lbltxtTotal" runat="server" Text="Total Price" />
            </FooterTemplate>
            <FooterTemplate>
                <asp:Label ID="lblTotal" runat="server" />
             </FooterTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="id" HeaderText="id" SortExpression="id"></asp:BoundField>
        <asp:ButtonField CommandName="Delete" Text="Delete" ButtonType="Button" ShowHeader="True" HeaderText="Delete"></asp:ButtonField>

    </Columns>
    <EmptyDataTemplate>No Record Available</EmptyDataTemplate>
    <FooterStyle BackColor="White" Font-Bold="True"></FooterStyle>
    <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White"></HeaderStyle>

    <PagerStyle HorizontalAlign="Right" BackColor="White" ForeColor="Black"></PagerStyle>

    <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White"></SelectedRowStyle>

    <SortedAscendingCellStyle BackColor="#F7F7F7"></SortedAscendingCellStyle>

    <SortedAscendingHeaderStyle BackColor="#4B4B4B"></SortedAscendingHeaderStyle>

    <SortedDescendingCellStyle BackColor="#E5E5E5"></SortedDescendingCellStyle>

    <SortedDescendingHeaderStyle BackColor="#242121"></SortedDescendingHeaderStyle>
</asp:GridView>

and here is a backend:

protected void Timer1_Tick(object sender, EventArgs e)
    {
        string Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
        string Username_new = Username.Replace("APAC\\", "");
        GridView1.DataBind();
        //live data
        String myquery = "Select * from Basket where Username='" + Username_new + "'";
        DataTable dt = new DataTable();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = myquery;
        cmd.Connection = con;
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;

        da.Fill(dt);
        GridView1.FooterRow.Cells[1].Text = "Total Amount";
        GridView1.FooterRow.Cells[2].Text = dt.Compute("Sum(price)", "").ToString();
    }
  • *"... I had an error."* It would really help us to help you if you described the error you get. – Filburt Aug 05 '18 at 07:13
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – VDWWD Aug 05 '18 at 09:41
  • @VDWWD what does it has similar to that topic? – Bogdan Zubar Aug 05 '18 at 09:49
  • 1
    Because in your question you had the following: "Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object.". But it has been removed in an edit. – VDWWD Aug 05 '18 at 19:01

1 Answers1

1

This is easy. Add method to your code:

  protected void getSUM()
{
// SQL query that gets total of product sales where category id = 1
string SqlQuery = @"SELECT SUM(ProductSales) AS TotalSales 
  FROM [NORTHWIND].[dbo].[Sales by Category] 
  WHERE CategoryID = 1";

// Declare and open a connection to database
SqlConnection conn = new SqlConnection(
ConfigurationManager.ConnectionStrings["NorthwindConnStr"].ConnectionString);
conn.Open();

// Creates SqlCommand object
SqlCommand comm = new SqlCommand(SqlQuery, conn);

// Gets total sales
decimal TotalSales = Convert.ToDecimal(comm.ExecuteScalar());

// Close connection
conn.Close();
conn.Dispose();
comm.Dispose();

// Adds formatted output to GridView footer
GridView1.Columns[1].FooterText = String.Format("{0:c}", TotalSales);
}

In markup code, we'll define footer template and call getSUM method:

  <FooterTemplate>
  Total sales:   <%# getSUM(); %>
  </FooterTemplate>
Mojtaba Nava
  • 568
  • 3
  • 14