EDIT: I'm sorry for multiple posts, I couldn't delete or modify the previous one.
Hey guys, ASP.net noob here (several years since I've worked with anything not opensource-related). I'm attempting to pretty much copy and modify an ecommerce site tutorial which is driven by my already created MS SQL database (running on sql server 2008r2).
I've based my project on the official ASP.net tutorial for Wingtip Toys, located here: http://www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/shopping-cart (this particular link is the shopping cart section I'm working on).
I've modified a few parts of this tutorial to work with my data, namely, I've change the portion of ShoppingCartActions.cs 'AddToCart' to use a decimal instead of an integer for the 'id' parameter (too large of a number to reasonably use an int type), that way I can change my id (acquired from URL parameter) to a string and compare it with my database entry (a string) to then find the correct row item.
Here is the main portion of the code in question...
ShoppingCartActions.cs (certain portions)
public void AddToCart(decimal id)
{
// Retrieve the product from the database.
ShoppingCartId = GetCartId();
var cartItem = _db.ShoppingCartItems.SingleOrDefault(
c => c.CartId == ShoppingCartId
&& c.ProductId == id);
if (cartItem == null)
{
var idHold = id.ToString();
// Create a new cart item if no cart item exists.
cartItem = new cartItem
{
ItemId = Guid.NewGuid().ToString(),
ProductId = id,
CartId = ShoppingCartId,
Product = _db.Products.SingleOrDefault(
p => p.generatedID == idHold),
Quantity = 1,
DateCreated = DateTime.Now
};
_db.ShoppingCartItems.Add(cartItem);
}
else
{
// If the item does exist in the cart,
// then add one to the quantity.
cartItem.Quantity++;
}
_db.SaveChanges();
}
...
public string GetCartId()
{
if (HttpContext.Current.Session[CartSessionKey] == null)
{
if (!string.IsNullOrWhiteSpace(HttpContext.Current.User.Identity.Name))
{
HttpContext.Current.Session[CartSessionKey] = HttpContext.Current.User.Identity.Name;
}
else
{
// Generate a new random GUID using System.Guid class.
Guid tempCartId = Guid.NewGuid();
HttpContext.Current.Session[CartSessionKey] = tempCartId.ToString();
}
}
return HttpContext.Current.Session[CartSessionKey].ToString();
}
...related files...
cartItem.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace projectName.Models
{
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 decimal ProductId { get; set; }
public virtual Tran Product { get; set; }
}
}
productContext.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace projectName.Models
{
public class productContext : DbContext
{
public productContext()
: base("projectName")
{
}
/* Products is a Tran string element, check ProductID sub-element in shoppingcartactions.cs */
public DbSet<Tran> Products { get; set; }
public DbSet<cartItem> ShoppingCartItems { get; set; }
}
}
AddToCart.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using projectName.Logic;
namespace projectName
{
public partial class AddToCart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string rawId = Request.QueryString["ProductID"];
decimal productId;
if (!String.IsNullOrEmpty(rawId) && decimal.TryParse(rawId, out productId))
{
using (ShoppingCartActions usersShoppingCart = new ShoppingCartActions())
{
usersShoppingCart.AddToCart(Decimal.Parse(rawId));
}
}
else
{
Debug.Fail("ERROR : We should never get to AddToCart.aspx without a ProductId.");
throw new Exception("ERROR : It is illegal to load AddToCart.aspx without setting a ProductId.");
}
Response.Redirect("ShoppingCart.aspx");
}
}
}
Everything seems to work fine until I attempt to save the newly created cartItem via the line in ShoppingCartActions.cs
_db.SaveChanges();
When I get to the point on the website that I add a new cartitem and display it (click add to cart), I get an error saying "An error occurred while updating the entries. See the inner exception for details"... the inner exception says "Arithmetic overflow error converting expression to data type int" pointing to this line of code
Let me know if there is anything else I can provide either file or concept related.. any ideas are much appreciated (been searching and playing for days)!