Input String was not in a correct format
Friends
I think this is due to some problem in my stored procedure.
Please, please take a look and tell me what is it that I am doing wrong. I have a product page from which users can select color, size, and width from three drop down lists. When they click "Add To Cart", the following routine fires:
private void processCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
if (e.CommandName.Equals("AddToCart"))
{
DropDownList ddlColor = (DropDownList)e.Item.FindControl("ddlColor");
DropDownList ddlSize = (DropDownList)e.Item.FindControl("ddlSize");
DropDownList ddlWidth = (DropDownList)e.Item.FindControl("ddlWidth");
Server.Transfer("PPAddToCart.aspx?ProductID=" + e.CommandArgument.ToString()+ "&shoecolor=" + ddlColor.SelectedValue.ToString() + "&shoesize=" + ddlSize.SelectedValue.ToString() + "&shoewidth=" + ddlWidth.SelectedValue.ToString());
}
}
As you can see, this is transferred to PPAddToCart.aspx which on Page Load does the following routine:
public PPAddToCart()
{
Page.Init += new System.EventHandler(Page_Init);
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (Request.Params["ProductID"] != null)
{
perrysplace.ppShoppingCartDB cart = new ppShoppingCartDB();
String CartId = cart.GetShoppingCartId();
String shoecolor = Request.QueryString["shoecolor"];
String shoesize = Request.QueryString["shoesize"];
String shoewidth = Request.QueryString["shoewidth"];
cart.AddItem(CartId, Int32.Parse(Request.Params["ProductID"]), 1, shoecolor, shoesize, shoewidth);
}
Response.Redirect("PPShoppingCart.aspx");
}
private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}
Here is the SPROC that performs the AddItem method, following which is the method itself.
CREATE Procedure PPShoppingCartAddItem
(
@CartID nvarchar(50),
@ProductID int,
@Quantity int,
@Color nvarchar(50) = null,
@ShoeSize nvarchar(50) = null,
@Width nvarchar(50) = null
)
As
IF LEN(RTRIM(LTRIM(@Color))) = 0 SELECT @Color = null
IF LEN(RTRIM(LTRIM(@ShoeSize))) = 0 SELECT @ShoeSize = null
IF LEN(RTRIM(LTRIM(@Width))) = 0 SELECT @Width = null
DECLARE @CountItems int
SELECT
@CountItems = Count(ProductID)
FROM
PPShoppingCart
WHERE
ProductID = @ProductID
AND
CartID = @CartID
IF @CountItems > 0 /* There are items - update the current quantity */
UPDATE
PPShoppingCart
SET
Quantity = (@Quantity + PPShoppingCart.Quantity)
WHERE
ProductID = @ProductID
AND
CartID = @CartID
ELSE /* New entry for this Cart. Add a new record */
INSERT INTO PPShoppingCart
(
CartID,
Quantity,
ProductID,
Color,
ShoeSize,
Width
)
VALUES
(
@CartID,
@Quantity,
@ProductID,
@Color,
@ShoeSize,
@Width
)
GO
Here is the AddItem method:
public void AddItem(string cartID, int productID, int quantity, string color, string size, string width)
{
//This method add items to shopping cart. Important method!!
SqlConnection cnCart = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand cmdSelect = new SqlCommand("PPShoppingCartAddItem", cnCart);
cmdSelect.CommandType = CommandType.StoredProcedure;
SqlParameter parameterProductID = new SqlParameter("@ProductID", SqlDbType.Int, 4);
parameterProductID.Value = productID;
cmdSelect.Parameters.Add(parameterProductID);
SqlParameter parameterCartID = new SqlParameter("@CartID", SqlDbType.NVarChar, 50);
parameterCartID.Value = cartID;
cmdSelect.Parameters.Add(parameterCartID);
SqlParameter parameterQuantity = new SqlParameter("@Quantity", SqlDbType.Int, 4);
parameterQuantity.Value = quantity;
cmdSelect.Parameters.Add(parameterQuantity);
SqlParameter parameterColor = new SqlParameter("@Color", SqlDbType.NVarChar, 50);
parameterQuantity.Value = color;
cmdSelect.Parameters.Add(parameterColor);
SqlParameter parameterShoeSize = new SqlParameter("@ShoeSize", SqlDbType.NVarChar, 50);
parameterQuantity.Value = size;
cmdSelect.Parameters.Add(parameterShoeSize);
SqlParameter parameterWidth = new SqlParameter("@Width", SqlDbType.NVarChar, 50);
parameterQuantity.Value = width;
cmdSelect.Parameters.Add(parameterWidth);
cnCart.Open();
cmdSelect.ExecuteNonQuery();
cnCart.Close();
}
The agony is that I had all this going very well about a week ago and then I formatted my harddrive without making any copies of the application.
Please, please take a look and tell me what is it that I am doing wrong. Thanks very much.
|