我使用的 reader 是错误的还是我的 reader 可能有空值?
Am I using reader wrong or is it possible my reader has null values?
我正在从事一个电子商务项目,但在使用 reader 时遇到了一些麻烦。这是我的 ShoppingCartController 代码,它不起作用
public List<PRODUCT> GetCartItems()
{
SqlConnection sqlConnection1 = new SqlConnection("MyConnection");
SqlCommand cmd = new SqlCommand("uspGetCart", sqlConnection1);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Username", SqlDbType.VarChar).Value = User.Identity.GetUserName();
sqlConnection1.Open();
SqlDataReader reader = cmd.ExecuteReader();
List<PRODUCT> cartList = new List<PRODUCT>();
PRODUCT product;
while (reader.Read())
{
product = new PRODUCT();
product.MANUFACTURER.ManufacturerName = reader["ManufacturerName"].ToString();
product.Name = reader["Name"].ToString();
product.Cost = decimal.Parse(reader["Cost"].ToString());
cartList.Add(product);
}
sqlConnection1.Close();
return cartList;
}
我得到的错误是
对象引用未设置到对象的实例。
堆栈跟踪是
[NullReferenceException: Object reference not set to an instance of an object.]
SeeSharpBeans.Controllers.ShoppingCartController.GetCartItems() in c:\Users\Andrew\Documents\Visual Studio 2013\Projects\SeeSharpBeans\SeeSharpBeans\Controllers\ShoppingCartController.cs:54
SeeSharpBeans.Controllers.ShoppingCartController.Index() in c:\Users\Andrew\Documents\Visual Studio 2013\Projects\SeeSharpBeans\SeeSharpBeans\Controllers\ShoppingCartController.cs:22
lambda_method(Closure , ControllerBase , Object[] ) +101
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +435
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +60
System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +76
System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +36
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +73
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +117
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +323
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +44
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +72
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
我的产品模型是这样的
public partial class PRODUCT
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public PRODUCT()
{
this.PURCHASES = new HashSet<Purchase>();
this.TRANSACTIONS = new HashSet<TRANSACTION>();
}
public int ProductID { get; set; }
public int ManufacturerID { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public decimal Cost { get; set; }
public virtual MANUFACTURER MANUFACTURER { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Purchase> PURCHASES { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<TRANSACTION> TRANSACTIONS { get; set; }
}
如果您的数据不包含空值,那么可能的问题是您没有将 MANUFACTURER
属性 初始化为 PRODUCT
构造函数中的值,这意味着它是当您尝试将 product.MANUFACTURER.ManufacturerName
设置为一个值时为 null。
另请注意,命名 类 和全部大写的成员通常是错误的形式 - 考虑将它们更改为 Product
和 Manufacturer
。
我认为问题可能出在行
product.MANUFACTURER.ManufacturerName = reader["ManufacturerName"].ToString();
MANUFACTURER 是否在产品 class 中初始化?
尝试将集合项定义移动到 while 循环的范围内:
while (reader.Read())
{
PRODUCT product = new PRODUCT();
product.MANUFACTURER.ManufacturerName = reader["ManufacturerName"].ToString();
product.Name = reader["Name"].ToString();
product.Cost = decimal.Parse(reader["Cost"].ToString());
cartList.Add(product); }
This line is the issue
product.MANUFACTURER.ManufacturerName = reader["ManufacturerName"].ToString();
Use
product.MANUFACTURER.ManufacturerName = Convert.ToString(reader["ManufacturerName"])
product.Cost = decimal.Parse(Convert.ToString(reader["Cost"]));
instead of .ToString();
需要使用它们的构造函数来初始化 MANUFACTURER,例如:-
while (reader.Read())
{
product = new PRODUCT();
product.MANUFACTURER=new MANUFACTURER();
product.MANUFACTURER.ManufacturerName = reader["ManufacturerName"].ToString();
product.Name = reader["Name"].ToString();
product.Cost = decimal.Parse(reader["Cost"].ToString());
cartList.Add(product);
}
我正在从事一个电子商务项目,但在使用 reader 时遇到了一些麻烦。这是我的 ShoppingCartController 代码,它不起作用
public List<PRODUCT> GetCartItems()
{
SqlConnection sqlConnection1 = new SqlConnection("MyConnection");
SqlCommand cmd = new SqlCommand("uspGetCart", sqlConnection1);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Username", SqlDbType.VarChar).Value = User.Identity.GetUserName();
sqlConnection1.Open();
SqlDataReader reader = cmd.ExecuteReader();
List<PRODUCT> cartList = new List<PRODUCT>();
PRODUCT product;
while (reader.Read())
{
product = new PRODUCT();
product.MANUFACTURER.ManufacturerName = reader["ManufacturerName"].ToString();
product.Name = reader["Name"].ToString();
product.Cost = decimal.Parse(reader["Cost"].ToString());
cartList.Add(product);
}
sqlConnection1.Close();
return cartList;
}
我得到的错误是 对象引用未设置到对象的实例。
堆栈跟踪是
[NullReferenceException: Object reference not set to an instance of an object.]
SeeSharpBeans.Controllers.ShoppingCartController.GetCartItems() in c:\Users\Andrew\Documents\Visual Studio 2013\Projects\SeeSharpBeans\SeeSharpBeans\Controllers\ShoppingCartController.cs:54
SeeSharpBeans.Controllers.ShoppingCartController.Index() in c:\Users\Andrew\Documents\Visual Studio 2013\Projects\SeeSharpBeans\SeeSharpBeans\Controllers\ShoppingCartController.cs:22
lambda_method(Closure , ControllerBase , Object[] ) +101
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +435
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +60
System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +76
System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +36
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +73
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +117
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +323
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +44
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +72
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
我的产品模型是这样的
public partial class PRODUCT
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public PRODUCT()
{
this.PURCHASES = new HashSet<Purchase>();
this.TRANSACTIONS = new HashSet<TRANSACTION>();
}
public int ProductID { get; set; }
public int ManufacturerID { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public decimal Cost { get; set; }
public virtual MANUFACTURER MANUFACTURER { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Purchase> PURCHASES { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<TRANSACTION> TRANSACTIONS { get; set; }
}
如果您的数据不包含空值,那么可能的问题是您没有将 MANUFACTURER
属性 初始化为 PRODUCT
构造函数中的值,这意味着它是当您尝试将 product.MANUFACTURER.ManufacturerName
设置为一个值时为 null。
另请注意,命名 类 和全部大写的成员通常是错误的形式 - 考虑将它们更改为 Product
和 Manufacturer
。
我认为问题可能出在行
product.MANUFACTURER.ManufacturerName = reader["ManufacturerName"].ToString();
MANUFACTURER 是否在产品 class 中初始化?
尝试将集合项定义移动到 while 循环的范围内:
while (reader.Read())
{
PRODUCT product = new PRODUCT();
product.MANUFACTURER.ManufacturerName = reader["ManufacturerName"].ToString();
product.Name = reader["Name"].ToString();
product.Cost = decimal.Parse(reader["Cost"].ToString());
cartList.Add(product); }
This line is the issue
product.MANUFACTURER.ManufacturerName = reader["ManufacturerName"].ToString();
Use
product.MANUFACTURER.ManufacturerName = Convert.ToString(reader["ManufacturerName"])
product.Cost = decimal.Parse(Convert.ToString(reader["Cost"]));
instead of .ToString();
需要使用它们的构造函数来初始化 MANUFACTURER,例如:-
while (reader.Read())
{
product = new PRODUCT();
product.MANUFACTURER=new MANUFACTURER();
product.MANUFACTURER.ManufacturerName = reader["ManufacturerName"].ToString();
product.Name = reader["Name"].ToString();
product.Cost = decimal.Parse(reader["Cost"].ToString());
cartList.Add(product);
}