Search code examples
c#.netreader

Am I using reader wrong or is it possible my reader has null values?


I'm working on an e-commerce project and am having a little trouble with the reader. Here is my ShoppingCartController code that is not working

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;
    }

The error that I'm getting is Object reference not set to an instance of an object.

Stack Trace is

    [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

My Product model looks like this

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; }
}

Solution

  • If your data does not contain null values then the likely problem is that you do not initialize the MANUFACTURER property to a value in the PRODUCT constructor, which means it is null when you attempt to set product.MANUFACTURER.ManufacturerName to a value.

    Also note that naming classes and members in ALL CAPS is generally bad form - consider changing them to Product and Manufacturer.