我正在遵循与此帖中相同的教程:
shoppingcart using gridview asp.net
但是我得到了一个不同的错误,我不明白为什么。当我运行应用程序时,它工作到我收到此异常消息:
“App_Web_ia5agzom.dll中发生了'System.NullReferenceException'类型的异常,但未在用户代码中处理”
并指向ShoppingCart.aspx中的以下行:
<%# String.Format("{0:c}", ((Convert.ToDouble(Item.Quantity)) * Convert.ToDouble(Item.Product.UnitPrice))) %>
如果我删除程序运行,但不显示每个单位的价格。
我的代码如下:
ShoppingCart.aspx:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/FrontEnd.Master" AutoEventWireup="true" CodeBehind="ShoppingCart.aspx.cs" Inherits="Gbl.UserPages.ShoppingCart" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="cphMain" runat="server">
<div id="ShoppingCartTitle" runat="server" class="ContentHead"><h1>Shopping Cart</h1></div>
<asp:GridView ID="CartList" runat="server"
AutoGenerateColumns="False"
ShowFooter="True"
GridLines="Vertical"
CellPadding="4"
ItemType="Gbl.Models.CartItem"
SelectMethod="GetShoppingCartItems"
CssClass="table table-striped table-bordered" >
<Columns>
<asp:BoundField DataField="ProductId" HeaderText="ID" SortExpression="ProductId" />
<asp:BoundField DataField="Product.ProductName" HeaderText="Name" />
<asp:BoundField DataField="Product.UnitPrice" HeaderText="Price (each)" DataFormatString="{0:c}"/>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="PurchaseQuantity" Width="40" runat="server" Text="<%#: Item.Quantity %>"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item Total">
<ItemTemplate>
<%# String.Format("{0:c}", ((Convert.ToDouble(Item.Quantity)) * Convert.ToDouble(Item.Product.UnitPrice))) %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remove Item">
<ItemTemplate>
<asp:CheckBox id="Remove" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<div>
<p></p>
<strong>
<asp:Label ID="LabelTotalText" runat="server" Text="Order Total: "></asp:Label>
<asp:Label ID="lblTotal" runat="server" EnableViewState="false"></asp:Label>
</strong>
</div>
<br />
</asp:Content>
代码背后:
public partial class ShoppingCart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (ShoppingCartEngine usersShoppingCart = new ShoppingCartEngine())
{
decimal cartTotal = 0;
cartTotal = usersShoppingCart.GetTotal();
if (cartTotal > 0)
{
// Display Total.
lblTotal.Text = String.Format("{0:c}", cartTotal);
}
else
{
LabelTotalText.Text = "labeltotaltext";
lblTotal.Text = "labeltotal";
ShoppingCartTitle.InnerText = "Shopping Cart is Empty";
}
}
}
public List<CartItem> GetShoppingCartItems()
{
ShoppingCartEngine engine = new ShoppingCartEngine();
return engine.GetCartItems();
}
}
CardItem.cs:
public class CartItem
{
[Key]
public string ItemId { get; set; }
public string CartId { get; set; }
public int Quantity { get; set; }
public System.DateTime DateCreated { get; set; }
public int ProductId { get; set; }
public Product Product { get; set; }
}
最后我的堆栈跟踪:
[NullReferenceException: Object reference not set to an instance of an object.]
ASP.userpages_shoppingcart_aspx.__DataBind__control17(Object sender, EventArgs e) in i:\gabriel\work\school\civ_eng\gral_workspaces\visualstudio2014_ASP.NET\assignment_2\Gbl\UserPages\ShoppingCart.aspx:25
System.Web.UI.Control.OnDataBinding(EventArgs e) +92
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +85
System.Web.UI.Control.DataBind() +15
System.Web.UI.Control.DataBindChildren() +187
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +94
System.Web.UI.Control.DataBind() +15
System.Web.UI.Control.DataBindChildren() +187
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +94
System.Web.UI.Control.DataBind() +15
System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +167
System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +3724
System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +67
System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +14
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +123
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +33
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +138
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +30
System.Web.UI.WebControls.GridView.DataBind() +4
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +105
System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +75
System.Web.UI.Control.EnsureChildControls() +83
System.Web.UI.Control.PreRenderRecursiveInternal() +42
System.Web.UI.Control.PreRenderRecursiveInternal() +155
System.Web.UI.Control.PreRenderRecursiveInternal() +155
System.Web.UI.Control.PreRenderRecursiveInternal() +155
System.Web.UI.Control.PreRenderRecursiveInternal() +155
System.Web.UI.Control.PreRenderRecursiveInternal() +155
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974
有人可以帮忙吗?
谢谢
答案 0 :(得分:0)
根据教程,该行应包含UnitPrice而不是Price:
<%#: String.Format("{0:c}", ((Convert.ToDouble(Item.Quantity)) * Convert.ToDouble(Item.Product.UnitPrice)))%>
答案 1 :(得分:0)
为了启用延迟加载,需要将Product设置为virtual:
public virtual Product Product { get; set; }
所以实体类应如下所示:
公共类CartItem {
[Key]
public string CartItemId { get; set; }
public string CartId { get; set; }
public int ProductId { get; set; }
public int Quantity { get; set; }
public System.DateTime DateCreated { get; set; }
public virtual Product Product { get; set; }
}