// Insert Product \
        public int InsertProduct(CProduct Product)
        {
            try
            {
                SqlConnection Connection = null;
                if (OpenDBConnection(ref Connection) == 0)
                {
                    SqlCommand Command = new SqlCommand("uspAddProduct", Connection);
                    int        intID   = -1;

                    SetParameter(ref Command, "@intProductID", 0, SqlDbType.Int, Direction: ParameterDirection.Output);
                    SetParameter(ref Command, "@strTitle", Product.Title, SqlDbType.VarChar);
                    SetParameter(ref Command, "@strDescription", Product.Description, SqlDbType.VarChar);
                    SetParameter(ref Command, "@intPrice", Product.Price, SqlDbType.Int);
                    SetParameter(ref Command, "@intStatusID", Product.StatusID, SqlDbType.Int);
                    SetParameter(ref Command, "@intCategoryID", Product.CategoryID, SqlDbType.Int);

                    Command.ExecuteReader();

                    intID             = (int)Command.Parameters["@intProductID"].Value;
                    Product.ProductID = intID;
                    CloseDBConnection(ref Connection);
                    return(0); // Success
                }
                else
                {
                    return(-1);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        // Update Product \
        public int UpdateProduct(CProduct Product)
        {
            try
            {
                SqlConnection Connection = null;

                if (OpenDBConnection(ref Connection) == 0)
                {
                    SqlCommand cm = new SqlCommand("uspEditProduct", Connection);

                    SetParameter(ref cm, "@intProductID", Product.ProductID, SqlDbType.Int);
                    SetParameter(ref cm, "@intCategoryID", Product.CategoryID, SqlDbType.Int);
                    SetParameter(ref cm, "@strTitle", Product.Title, SqlDbType.VarChar);
                    SetParameter(ref cm, "@strDescription", Product.Description, SqlDbType.VarChar);
                    SetParameter(ref cm, "@intPrice", Product.Price, SqlDbType.Int);
                    SetParameter(ref cm, "@intStatusID", Product.StatusID, SqlDbType.Int);

                    cm.ExecuteReader();
                    CloseDBConnection(ref Connection);

                    return(0); // Success
                }
                else
                {
                    return(-1); // No database connection
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Example #3
0
        // Get Product List \
        public List <CProduct> GetProducts(int intProductID = 0, int intCategoryID = -1)
        {
            SqlConnection   Connection  = new SqlConnection();
            List <CProduct> ProductList = new List <CProduct>();

            try
            {
                if (OpenDBConnection(ref Connection) == 0)
                {
                    SqlDataAdapter da = new SqlDataAdapter("uspSelectProduct", Connection);
                    da.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
                    DataSet ds = new DataSet();
                    int     intStatusID;

                    if (intProductID != 0)
                    {
                        SetParameter(ref da, "@intProductID", intProductID, SqlDbType.Int);
                    }

                    if (intCategoryID > -1)
                    {
                        SetParameter(ref da, "@intCategoryID", intCategoryID, SqlDbType.Int);
                    }
                    try
                    {
                        da.Fill(ds);

                        foreach (DataTable dt in ds.Tables)
                        {
                            foreach (DataRow dr in dt.Rows)
                            {
                                CProduct Product = new CProduct();
                                Product.ProductID      = Convert.ToInt32((dr["intProductID"]));
                                Product.Title          = (dr["strTitle"]).ToString();
                                Product.Description    = (dr["strDescription"]).ToString();
                                Product.Price          = Convert.ToDouble((dr["intPrice"]));
                                Product.CategoryID     = Convert.ToInt32((dr["intCategoryID"]));
                                Product.PrimaryImage   = GetPrimaryImage(Product.ProductID);
                                Product.intStockAmount = Convert.ToInt32((dr["intStockAmount"]));

                                ProductList.Add(Product);
                            }
                        }
                    }
                    finally
                    {
                        CloseDBConnection(ref Connection);
                    }
                }
                return(ProductList);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List <CProduct> GetCart(int intUserID)
        {
            try
            {
                SqlConnection   Connection   = null;
                CProduct        Product      = new CProduct();
                List <CProduct> Cart         = new List <CProduct>();
                int             intProductID = 0;

                if (OpenDBConnection(ref Connection) == 0)
                {
                    SqlDataAdapter da = new SqlDataAdapter("uspGetCart", Connection);
                    da.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
                    DataSet ds = new DataSet();

                    SetParameter(ref da, "@intUserID", intUserID, SqlDbType.Int);

                    da.Fill(ds);

                    foreach (DataTable dt in ds.Tables)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            intProductID = (int)dr["intProductID"];
                            Product      = GetProduct(intProductID);
                            Cart.Add(Product);
                        }
                    }
                }

                CloseDBConnection(ref Connection);
                return(Cart);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Example #5
0
 public void Products()
 {
     CProduct ProductList = new CProduct();
 }
        // Get Product List \
        public List <CProduct> GetProducts(int intProductID = 0)
        {
            SqlConnection   Connection  = new SqlConnection();
            List <CProduct> ProductList = new List <CProduct>();

            try
            {
                if (OpenDBConnection(ref Connection) == 0)
                {
                    SqlDataAdapter da = new SqlDataAdapter("uspSelectProduct", Connection);
                    da.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
                    DataSet ds = new DataSet();
                    int     intCategoryID;
                    int     intStatusID;

                    if (intProductID != 0)
                    {
                        SetParameter(ref da, "@intProductID", intProductID, SqlDbType.Int);
                    }

                    try
                    {
                        da.Fill(ds);

                        foreach (DataTable dt in ds.Tables)
                        {
                            foreach (DataRow dr in dt.Rows)
                            {
                                CProduct Product = new CProduct();
                                Product.ProductID    = Convert.ToInt32((dr["intProductID"]));
                                Product.Title        = (dr["strTitle"]).ToString();
                                Product.Description  = (dr["strDescription"]).ToString();
                                Product.Price        = Convert.ToDouble((dr["intPrice"]));
                                intStatusID          = Convert.ToInt32((dr["intStatusID"]));
                                intCategoryID        = Convert.ToInt32((dr["intCategoryID"]));
                                Product.PrimaryImage = GetPrimaryImage(Product.ProductID);

                                switch (intStatusID)
                                {
                                case 0:
                                    Product.StatusID = StatusTypes.Unknown;
                                    break;

                                case 1:
                                    Product.StatusID = StatusTypes.Active;
                                    break;

                                case 2:
                                    Product.StatusID = StatusTypes.Sold;
                                    break;

                                case 3:
                                    Product.StatusID = StatusTypes.Closed;
                                    break;
                                }

                                switch (intCategoryID)
                                {
                                case 0:
                                    Product.CategoryID = ProductTypes.Unknown;
                                    break;

                                case 1:
                                    Product.CategoryID = ProductTypes.Necklace;
                                    break;

                                case 2:
                                    Product.CategoryID = ProductTypes.Bracelet;
                                    break;

                                case 3:
                                    Product.CategoryID = ProductTypes.Earring;
                                    break;

                                case 4:
                                    Product.CategoryID = ProductTypes.Ring;
                                    break;

                                case 5:
                                    Product.CategoryID = ProductTypes.Other;
                                    break;
                                }
                                ProductList.Add(Product);
                            }
                        }
                    }
                    finally
                    {
                        CloseDBConnection(ref Connection);
                    }
                }
                return(ProductList);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }