Problem with adding to cart / solved

Jan 31, 2011 at 7:35 AM

I get the following error when trying to add an album to the cart in the VB version I'm converting.
I'm using SQL Server in this version.
The error occurs at the HTML.ActionLink.
Object reference not set to an instance of an object.

@For Each item In Model.CartItems
 @<tr id="row-@item.RecordId">
      @Html.ActionLink(item.Album.Title,"Details", "Store", New With {.id = item.AlbumId}, Nothing )

When I try to check the completed C# solution I get the following error at the SaveChanges point. The completed solution is using the file DB
{"Cannot insert the value NULL into column 'RecordId', table 'MvcMusicStores.dbo.Carts'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}

public void AddToCart(Album album)
            // Get the matching cart and album instances
            var cartItem = storeDB.Carts.SingleOrDefault(c => c.CartId == ShoppingCartId && c.AlbumId == album.AlbumId);

            if (cartItem == null)
                // Create a new cart item if no cart item exists
                cartItem = new Cart
                    AlbumId = album.AlbumId,
                    CartId = ShoppingCartId,
                    Count = 1,
                    DateCreated = DateTime.Now

                // If the item does exist in the cart, then add one to the quantity

            // Save changes

Jan 31, 2011 at 9:08 PM

Both of these were the result of typos

The first I had a property of Albums instead of Album in the Genre Class.

The second I had forgot to put the original db file back in place from when I had an earlier issue with the PluralizingTableNameConvention, and was still referencing the other file in web.config.


Jan 31, 2011 at 11:28 PM

Glad you got it sorted out. That's the tradeoff of using convention-based systems like EF Code-First - if you mistype something, it won't work and it's hard to know exactly why.