Click here to Skip to main content
15,903,854 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
hi i tried for three pages but am getting total sum in grid footer..
but in other two pages am not getting sum..
error "Object reference is not set to an instance of an object"
can any one help me..
here is my code..
protected void grdCurrencyProfit_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           decimal rowTotal1 = Convert.ToDecimal
           (DataBinder.Eval(e.Row.DataItem, "tTotalAmount"));
           grdTotal1 = grdTotal1 + rowTotal1;
           decimal rowTotal2 = Convert.ToDecimal
           (DataBinder.Eval(e.Row.DataItem, "tSales"));
           grdTotal2 = grdTotal2 + rowTotal2;
           decimal rowTotal3 = Convert.ToDecimal
           (DataBinder.Eval(e.Row.DataItem, "tProfitAmount"));
           grdTotal3 = grdTotal3 + rowTotal3;

       }

       if (e.Row.RowType == DataControlRowType.Footer)
       {
           Label lbl1 = (Label)e.Row.FindControl("lblPurchase");
           Label lbl2 = (Label)e.Row.FindControl("lblSale");
           Label lbl3 = (Label)e.Row.FindControl("lblProfit");
           lbl1.Text = grdTotal1.ToString("c");
           lbl2.Text = grdTotal2.ToString("c");
           lbl3.Text = grdTotal3.ToString("c");

       }
   }


before page load.
C#
public partial class Ticketing : System.Web.UI.Page
{
    decimal grdTotal1 = 0;
    decimal grdTotal2 = 0;
    decimal grdTotal3 = 0;



and in grid html code
XML
<asp:GridView ID="grdCurrencyProfit" runat="server" AutoGenerateColumns="false" Width="333px"
                                 AllowPaging="True" AllowSorting="True" PageSize="5" OnPageIndexChanging="grdCurrencyProfit_PageIndexChanging"
                                 ShowFooter="true" BackColor="#ffffff" BorderColor="AliceBlue" BorderStyle="None"
                                 BorderWidth="1px" CellPadding="3" CellSpacing="2" FooterStyle-BackColor="#da821e"
                                 FooterStyle-ForeColor="#ffffff" RowStyle-BackColor="#003366" RowStyle-ForeColor="#ffffff"
                                 AlternatingRowStyle-BackColor="#da821e" OnRowDataBound="grdCurrencyProfit_RowDataBound">
                                 <Columns>
                                     <asp:TemplateField HeaderText="Subject" FooterText="TOTAL">
                                         <ItemTemplate>
                                             <%# DataBinder.Eval(Container.DataItem, "tSubject")%>
                                         </ItemTemplate>
                                     </asp:TemplateField>
                                     <asp:TemplateField HeaderText="Total Amount">
                                         <ItemTemplate>
                                         <asp:Label ID="lblPurchase" runat="server" Text='<%# "Rs"+Eval("tTotalAmount").ToString()%>'>
                                             </asp:Label>
                                            </ItemTemplate>

                                     </asp:TemplateField>
                                     <asp:TemplateField HeaderText="Sales Amount">
                                         <ItemTemplate>
                                          <asp:Label ID="lblSale" runat="server" Text='<%# "Rs"+Eval("tSales").ToString()%>'>
                                             </asp:Label>

                                         </ItemTemplate>
                                     </asp:TemplateField>
                                     <asp:TemplateField HeaderText="Profit">
                                         <ItemTemplate>
                                         <asp:Label ID="lblProfit" runat="server" Text='<%# "Rs"+Eval("tProfitAmount").ToString()%>'>
                                             </asp:Label>

                                         </ItemTemplate>
                                     </asp:TemplateField>
                                     <asp:TemplateField HeaderText="Date">
                                         <ItemTemplate>
                                             <%# DataBinder.Eval(Container.DataItem, "tDateofJourney")%>
                                         </ItemTemplate>
                                     </asp:TemplateField>
                                 </Columns>
                                 <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                                 <HeaderStyle BackColor="#da821e" Font-Bold="True" ForeColor="White" />

                             </asp:GridView>



am not able to solve this small issue..can any one suggest me??

thanks
Posted
Updated 27-May-12 18:51pm
v3
Comments
db7uk 27-May-12 15:52pm    
This is similar to how I have done it. Where is the exception being thrown? Also silly question, the gridview is being re-bound on the new page request?
ythisbug 28-May-12 0:50am    
thanks for ur reply..
lbl1.Text = grdTotal1.ToString("c");
am getting error in this line..

You do not show where lbl1 is declared, and where grdTotal1 is declared (this is not Ticketing.grdTotal1), so what's the use of your question?

After all, this exception is the easiest to figure out. Set a break point on the line where the exception is thrown. Before executing this line, check up the variables/fields involved; one or more of them is null. Make sure the variable or field is initialized before this line is executed, or check up it for null and make the execution of the line conditional, execute it only all involved variables/members are non-null.

—SA
 
Share this answer
 
v2
Comments
ythisbug 28-May-12 1:25am    
thanks.
i put breakpoint in lbl1,lbl2,lbl3 value coming null..
and in grdTotal1,grdTotal2,grdTotal3 getting value.
Sergey Alexandrovich Kryukov 28-May-12 16:54pm    
All right, now fix your logic accordingly. And consider accepting my answer formally (green button) -- thanks.
--SA
Object reference not set to an instance of an object
This error happens when you try to use a property or call a method of an object that is null. More details: here[^]



Issue here:
You failed to define your FOOTER TEMPLATES in the designer. You try to access the item templates in the code behind with an if condition for footer row. Hence you get the null labels and the error. Handle it. Define the footer template and use it.



better code for this?
Have a look at this sample here for reference:
MSDN: GridView Examples for ASP.NET 2.0: Displaying Summary Data in the Footer[^]
 
Share this answer
 
Comments
ythisbug 28-May-12 7:10am    
thanks u sandeep.i will try
Sandeep Mewara 28-May-12 8:07am    
Welcome.

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



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