public static string AddToCart(Cart cart, int productID, int quantity) { string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString; SqlConnection connection = new SqlConnection(connectionString); String sql = "INSERT INTO [ShoppingCart] VALUES(@CustomerID, @CartID, @ProductID, @Quantity)"; try { connection.Open(); SqlCommand command = new SqlCommand(sql, connection); command.Parameters.Add("@CustomerID", SqlDbType.Int).Value = cart.CustomerID; command.Parameters.Add("@ProductID", SqlDbType.Int).Value = productID; command.Parameters.Add("@Quantity", SqlDbType.Int).Value = quantity; command.Parameters.Add("@CartID", SqlDbType.Int).Value = cart.CartID; command.ExecuteNonQuery(); connection.Close(); return "Complete"; } catch (SqlException sqlEx) { return (sqlEx.Message); } finally { connection.Close(); } }
public static Boolean checkWithAccessory(Cart cart, int custID) { List<int> pIDs = new List<int>(); Boolean hasPhone = false, hasAccessory = false, hasBoth = false; string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString; SqlConnection connection = new SqlConnection(connectionString); String sql = "SELECT ProductID FROM [ShoppingCart] WHERE CustomerID = @CustomerID"; SqlCommand command = new SqlCommand(sql, connection); SqlDataReader reader; connection.Open(); command.Parameters.Add("@CustomerID", SqlDbType.Int); command.Parameters["@CustomerID"].Value = custID; reader = command.ExecuteReader(); while (reader.HasRows) { while (reader.Read()) { pIDs.Add(reader.GetInt32(0)); } reader.NextResult(); } connection.Close(); for (int i = 0; i < pIDs.Count; i++) { if (StandardProduct.getProductType(pIDs[i]).Contains("Phone")) hasPhone = true; else if (StandardProduct.getProductType(pIDs[i]).Contains("Accessory")) hasAccessory = true; } if (hasPhone && hasAccessory) hasBoth = true; return hasBoth; }
protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e) { if (sessionCust != null) { sessionCart = new Cart(sessionCust.CustomerID, 1); //Get the button clicked. Button btnAddToCart = ((Button)e.CommandSource); //get the row the button lives in GridViewRow currentRow = ((GridViewRow)btnAddToCart.NamingContainer); //find the quantity text box TextBox txtQuantity = ((TextBox)currentRow.FindControl("txtQuantity")); Int32 qty = Convert.ToInt32(txtQuantity.Text); //get the row's datakey value by keyname using .Values["keyname"] or if you only have one datakey field you can just use .Value Int32 prodId = Convert.ToInt32(GridView3.DataKeys[currentRow.RowIndex].Values["ProductID"].ToString()); //pass the quantity value and the prodid to your AddToCart method. //CartFunctions.AddItemToCart(prodId, txtQuantity.Text, 0.0M); sessionCart.AddToCart(prodId, qty); //Response.Write for testing only.. lblAddToCart.Text = txtQuantity.Text + " of ProductID " + prodId + " added to cart."; } else Response.Write("Error: no customer is in session."); }
public static string RemoveFromCart(Cart cart, int productID) { string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString; SqlConnection connection = new SqlConnection(connectionString); String sql = "DELETE FROM [ShoppingCart] WHERE CustomerID = @CustomerID AND ProductID = @ProductID"; SqlCommand command = new SqlCommand(sql, connection); try { command.Parameters.Add("@CustomerID", SqlDbType.Int); command.Parameters["@CustomerID"].Value = cart.CustomerID; command.Parameters.Add("@ProductID", SqlDbType.Int); command.Parameters["@ProductID"].Value = productID; connection.Open(); command.ExecuteNonQuery(); connection.Close(); return "Complete"; } catch (SqlException sqlEx) { return (sqlEx.Message); } finally { connection.Close(); } }
public static string getCartID(Cart cart) { int cartID = 0; String sql = "SELECT MAX(CartID) as MAX FROM [ShoppingCart]"; string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString; SqlConnection connection = new SqlConnection(connectionString); SqlCommand command = new SqlCommand(sql, connection); SqlDataReader reader; command = new SqlCommand(sql, connection); try { connection.Open(); reader = command.ExecuteReader(); while (reader.Read()) { cartID = reader.GetInt32(reader.GetOrdinal("MAX")); } cartID++; cart.CartID = cartID; reader.Close(); return "Complete"; } catch (SqlException sqlEx) { return (sqlEx.Message); } finally { connection.Close(); } }
public static string EditCart(Cart cart) { string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString; SqlConnection connection = new SqlConnection(connectionString); String sql = "UPDATE[ShoppingCart] SET CustomerID = @CustomerID, ProductID = NULL, Quantity = NULL, CartID = @CartID"; try { connection.Open(); SqlCommand command = new SqlCommand(sql, connection); command.Parameters.Add("@CartID", SqlDbType.Int); command.Parameters["@CartID"].Value = cart.CartID; command.Parameters.Add("@CustomerID", SqlDbType.Int); command.Parameters["@CustomerID"].Value = cart.CustomerID; command.ExecuteNonQuery(); connection.Close(); return "Complete"; } catch (SqlException sqlEx) { return (sqlEx.Message); } }
public static string CreateCart(Cart cart) { string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString; SqlConnection connection = new SqlConnection(connectionString); String sql = "INSERT INTO [ShoppingCart] VALUES(@CustomerID, NULL, NULL, @CartID)"; try { connection.Open(); SqlCommand command = new SqlCommand(sql, connection); command.Parameters.Add("@CartID", SqlDbType.Int); command.Parameters["@CartID"].Value = cart.CartID; command.Parameters.Add("@CustomerID", SqlDbType.Int); command.Parameters["@CustomerID"].Value = cart.CustomerID; command.ExecuteNonQuery(); connection.Close(); return "Complete"; } catch (SqlException sqlEx) { return (sqlEx.Message); } }