Gridview Total获取NullReferenceError对象引用未设置为对象的实例。 [英] Gridview Total getting NullReferenceError Object reference not set to an instance of an object.

查看:59
本文介绍了Gridview Total获取NullReferenceError对象引用未设置为对象的实例。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在gridview的页脚中得到一个总数。它曾经在那里,但当我在桌子上添加一些按钮时消失了。



这是我的代码:

 <   asp:GridView    < span class =code-attribute> ID   =  gvBasket    runat   =  server    AutoGenerateColumns   =  False  
ShowFooter = True onrowdatabound = gvBasket_RowDataBound >
< FooterStyle Font-Bold = true < span class =code-attribute> BackColor = #61A6F8 ForeColor = 黑色 / >
< >
< asp:BoundField DataField = ProductName HeaderText < span class =code-keyword> = 产品名称 SortExpression = ProductName / >
< asp:BoundField DataField = qty HeaderText = 数量 SortExpression = qty / >
< asp:BoundField DataField = UnitPrice HeaderText = Unit Prce SortExpression = unitPrice
< span class =code-attribute> HtmlEncode = false DataFormatString = {0:c} / >
< asp:TemplateField HeaderText = 总计 SortExpression = 总计 >
< ItemTemplate > ;
< asp:标签 ID = lblTotal runat = server 文字 =' <% #Eval( Total 0:c%> ' / < span class =code-keyword>>
< / ItemTemplate >
< FooterTemplate >
< asp:标签 ID = lblsubTotal runat = server / >

< / FooterTemplate >
< / asp:TemplateField >
< asp:BoundField DataField = sessionID HeaderText = 会话
SortExpression = sessionID 可见 = false / > ;
< asp:BoundField DataField = UserID HeaderText = user SortExpression = UserID 可见 = false / >
< asp:BoundField DataField = ProductID HeaderText = ProductID
< span class =code-attribute> SortExpression = ProductID 可见 = false / >
< asp :TemplateField HeaderText = 修改 >
< ItemTemplate >
< asp:按钮 ID = 删除 runat = server
CommandArgument =' <% #Bind( productID%> ' onclick = Delete_Click
文本 CommandName = DeleteItem / >
< asp:按钮 ID = btnAdd runat = server
CommandArgument =' <% #Bind( productID%> ' CommandName = AddItem
onclick = btnAdd_Click 文字 = 添加 / >
< / ItemTemplate >
< / asp:TemplateField >
< / Columns >
< / asp:GridView >





和背后的代码是:



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







我希望Total total列有一个底部的小计。



我的表格代码是:

  string  session = Session.SessionID.ToString(); 

string Query = 选择tblProduct.ProductName作为ProductName,tblProduct.UnitPrice作为UnitPrice,sum(ShoppingBasket.qty)作为数量,tblProduct.UnitPrice * sum(ShoppingBasket.qty)作为Total,ShoppingBasket.sessionID,ShoppingBasket.userID,ShoppingBasket.productID FROM ShoppingBasket join tblProduct在ShoppingBasket.productID = tblProduct.ProductID上,其中ShoppingBasket.sessionID =' + session + '和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();

解决方案

您没有显示带有对象引用未设置为对象实例消息的异常位置抛出。



不用担心。这是检测和修复的最简单的案例之一。它只是意味着某些引用类型的某个成员/变量通过使用和它的实例(非静态)成员解除引用,这要求此成员/变量为非null,但实际上它似乎为null。只需在调试器下执行它,它将停止抛出异常的执行。在该行上设置一个断点,重新启动应用程序并再次到达这一点。评估下一行中涉及的所有引用,并查看哪一个为null,而不需要为null。解决这个问题之后,修复代码:确保将成员/变量正确初始化为非空引用,或者将其检查为null,如果为null,则执行其他操作。



另请参阅:想要在按钮点击时显示下一条记录。但是如果下一条记录功能的条件对象引用没有设置为对象的实例 [ ^ ]。



祝你好运,

-SA


我从您的代码中找到的是页脚,您已经取了标签lblSubTotal:



< pre lang =xml> < FooterTemplate >
< asp:Label ID = lblsubTotal runat = server / >

< / FooterTemplate >





但是在使用lblTotal访问的代码中:



  if (e.Row.RowType == DataControlRowType.Footer)
{
标签lblTotal =(标签)e.Row.FindControl( lblTotal);
lblTotal.Text = stotal.ToString();
}





因此它会抛出异常,说明找不到对象引用。



尝试将标签的正确名称放在以下位置:



标签lblTotal =(标签)e .Row.FindControl(    lblSubTotal   ); 





希望这会有所帮助。


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: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:

        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:

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();

解决方案

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


What I found from your code is in footer you have taken the label lblSubTotal:

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

               </FooterTemplate>



But in code you are accessing using lblTotal:

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:

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



Hope this helps.


这篇关于Gridview Total获取NullReferenceError对象引用未设置为对象的实例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆