Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to get a total in the footer of gridview. It was there before but dissappeared when I added some buttons to the table.

Here is my code:
ASP
<asp:GridView ID="gvBasket" runat="server" AutoGenerateColumns="False"
    ShowFooter="True" onrowdatabound="gvBasket_RowDataBound">
    <FooterStyle Font-Bold="true" BackColor="#61A6F8" ForeColor="black" />
 <Columns>
        <asp:BoundField DataField="ProductName" HeaderText="Product Name" SortExpression="ProductName" />
        <asp:BoundField DataField="qty" HeaderText="Quantity" SortExpression="qty" />
        <asp:BoundField DataField="UnitPrice" HeaderText="Unit Prce" SortExpression="unitPrice"
            HtmlEncode="false" DataFormatString="{0:c}" />
        <asp:TemplateField HeaderText="Total" SortExpression="Total">
            <ItemTemplate>
                <asp:Label ID="lblTotal" runat="server" Text='<%# Eval("Total","0:c") %>'  />
            </ItemTemplate>
            <FooterTemplate>
            <asp:Label ID="lblsubTotal" runat="server" />

            </FooterTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="sessionID" HeaderText="Session"
            SortExpression="sessionID" visible="false"/>
        <asp:BoundField DataField="UserID" HeaderText="user" SortExpression="UserID" Visible="false" />
        <asp:BoundField DataField="ProductID" HeaderText="ProductID"
            SortExpression="ProductID" visible="false"/>
        <asp:TemplateField HeaderText="Edit">
            <ItemTemplate>
                <asp:Button ID="Delete" runat="server"
                    CommandArgument='<%# Bind("productID") %>' onclick="Delete_Click"
                    Text="Delete" CommandName="DeleteItem" />
                <asp:Button ID="btnAdd" runat="server"
                    CommandArgument='<%# Bind("productID") %>' CommandName="AddItem"
                    onclick="btnAdd_Click" Text="Add" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>


and code behind is:

C#
        new decimal stotal = 0; 
        protected void gvBasket_RowDataBound(object sender, GridViewRowEventArgs e)       
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
Label lblTotal = (Label)e.Row.FindControl("lblTotal");
decimal total = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Total"));
lblTotal.Text = total.ToString();
stotal += total;
 }
 if (e.Row.RowType == DataControlRowType.Footer)
 {
Label lblTotal = (Label)e.Row.FindControl("lblTotal");
lblTotal.Text = stotal.ToString();
 }
}



I want the total Total column to have a subtotal at the bottom.

my code for the table is:
C#
string session = Session.SessionID.ToString();

             string Query = "SELECT  tblProduct.ProductName as ProductName, tblProduct.UnitPrice as UnitPrice, sum(ShoppingBasket.qty) as Qty , tblProduct.UnitPrice*sum(ShoppingBasket.qty) as Total, ShoppingBasket.sessionID, ShoppingBasket.userID, ShoppingBasket.productID FROM ShoppingBasket join tblProduct on ShoppingBasket.productID=tblProduct.ProductID where ShoppingBasket.sessionID= '" + session + "' AND ShoppingBasket.UserID='' group by ShoppingBasket.userID, tblProduct.ProductName, tblProduct.UnitPrice, ShoppingBasket.sessionID, ShoppingBasket.productID";



             ConnectionHandler objHandler = new ConnectionHandler();
             DataTable dt = objHandler.ExecuteSelect(Query);


             gvBasket.DataSource = dt;
             gvBasket.DataBind();
Posted
Updated 11-Dec-12 23:52pm
v2

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: wither make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Good luck,
—SA
 
Share this answer
 
What I found from your code is in footer you have taken the label lblSubTotal:

XML
<FooterTemplate>
               <asp:Label ID="lblsubTotal" runat="server" />

               </FooterTemplate>


But in code you are accessing using lblTotal:

C#
if (e.Row.RowType == DataControlRowType.Footer)
 {
Label lblTotal = (Label)e.Row.FindControl("lblTotal");
lblTotal.Text = stotal.ToString();
 }


so it is throwing an exception stating that object reference not found.

try putting correct name of the label at the following place:

C#
Label lblTotal = (Label)e.Row.FindControl("lblSubTotal");


Hope this helps.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900