I get this error and I don't know how to resolve it.
The stack trace :
[ArgumentNullException: Value cannot be null. Parameter name: source] System.Linq.Enumerable.FirstOrDefault(IEnumerable'1 source, Func'2 predicate) +4358562
eCommerce.Services.BasketService.addToBasket(HttpContextBase httpcontext, Int32 productid, Int32 quantity) in E:\projects\C#\eCommerce\eCommerce.Services\BasketService.cs:51
eCommerce.WebUI.Controllers.HomeController.AddToBasket(Int32 id) in E:\projects\C#\eCommerce\eCommerce.WebUI\Controllers\HomeController.cs:34
This is the code :
public bool addToBasket(HttpContextBase httpcontext, int productid, int quantity)
{
bool success = true;
Basket basket = GetBasket(httpcontext);
// this line throws the error
BasketItem item = basket.BasketItems.FirstOrDefault(i => i.ProductId == productid);
if (item == null)
{
item = new BasketItem()
{
BasketId = basket.BasketId,
ProductId = productid,
Quantity = quantity
};
basket.BasketItems.Add(item);
}
else
{
item.Quantity = item.Quantity + quantity;
}
baskets.Commit();
return success;
}
Please help me I have been stuck for a while now
BasketItems
is notnull
– user3559349