} // End of the GetEmailAddresses method #endregion #region Delete methods /// <summary> /// Delete a customer post on id /// </summary> /// <param name="id">The id number for the customer</param> /// <returns>An error code</returns> public static Int32 DeleteOnId(Int32 id) { // Delete reviews by customer id (Update product rating) Int32 page = 1; List<ProductReview> productReviews = ProductReview.GetByCustomerId(id, 10, page, "id", "ASC"); while (productReviews.Count > 0) { for (int i = 0; i < productReviews.Count; i++) { ProductReview.DeleteOnId(productReviews[i].id); Product.UpdateRating(productReviews[i].product_id, productReviews[i].language_id); } // Increase the page number and get more posts page = page + 1; productReviews = ProductReview.GetByCustomerId(id, 10, page, "id", "ASC"); } // Delete orders by customer id page = 1; List<Order> orders = Order.GetByCustomerId(id, 10, page, "id", "ASC"); while (orders.Count > 0) { for (int i = 0; i < orders.Count; i++) { Order.DeleteOnId(orders[i].id); } // Increase the page number and get more posts page = page + 1; orders = Order.GetByCustomerId(id, 10, page, "id", "ASC"); } // Create the connection and the sql statement string connection = Tools.GetConnectionString(); string sql = "DELETE FROM dbo.customers_files WHERE customer_id = @id;DELETE FROM dbo.customers WHERE id = @id;"; // The using block is used to call dispose automatically even if there is a exception using (SqlConnection cn = new SqlConnection(connection)) { // The using block is used to call dispose automatically even if there is a exception using (SqlCommand cmd = new SqlCommand(sql, cn)) { // Set command timeout to 90 seconds cmd.CommandTimeout = 90; // Add parameters cmd.Parameters.AddWithValue("@id", id); // The Try/Catch/Finally statement is used to handle unusual exceptions in the code to // avoid having our application crash in such cases try { // Open the connection. cn.Open(); // Execute the update cmd.ExecuteNonQuery(); } catch (SqlException e) { // Check for a foreign key constraint error if (e.Number == 547) { return 5; } else { throw e; } } catch (Exception e) { throw e; } } } // Return the code for success return 0; } // End of the DeleteOnId method