Exemplo n.º 1
0
    } // End of the GetBySearch method

    #endregion

    #region Delete methods

    /// <summary>
    /// Delete a option type post on id
    /// </summary>
    /// <param name="id">The id number for the option type post</param>
    /// <returns>An error code</returns>
    public static Int32 DeleteOnId(int id)
    {
        // Delete options by option type id
        List<Option> options = Option.GetByOptionTypeId(id);
        for (int i = 0; i < options.Count; i++)
        {
            ProductOption.DeleteOnOptionId(options[i].id);
            Option.DeleteOnId(options[i].id);
        }

        // Delete product option types
        List<ProductOptionType> productOptionTypes = ProductOptionType.GetByOptionTypeId(id);
        for (int i = 0; i < productOptionTypes.Count; i++)
        {
            ProductOptionType.DeleteOnId(productOptionTypes[i].id);
        }

        // Create the connection and the sql statement
        string connection = Tools.GetConnectionString();
        string sql = "DELETE FROM dbo.option_types_detail WHERE option_type_id = @id;DELETE FROM dbo.option_types 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