Exemplo n.º 1
0
 /*
 public bool AddProduct(Product displayP)
 {
     SqlProduct p = new SqlProduct(displayP);
     hasError = false;
     SqlConnection con = new SqlConnection(conString);
     SqlCommand cmd = new SqlCommand("AddProduct", con);
     cmd.CommandType = CommandType.StoredProcedure;
     cmd.Parameters.Add("@ModelNumber", SqlDbType.VarChar, prodStringLen);
     cmd.Parameters["@ModelNumber"].Value = p.ModelNumber;
     cmd.Parameters.Add("@ModelName", SqlDbType.VarChar, prodStringLen);
     cmd.Parameters["@ModelName"].Value = p.ModelName;
     cmd.Parameters.Add("@UnitCost", SqlDbType.Decimal);
     cmd.Parameters["@UnitCost"].Value = p.UnitCost;
     cmd.Parameters.Add("@Description", SqlDbType.VarChar, 200);
     cmd.Parameters["@Description"].Value = p.Description;
     if (p.Description==null)  cmd.Parameters["@Description"].Value = DBNull.Value;
     cmd.Parameters.Add("@CategoryName", SqlDbType.VarChar, prodStringLen);
     cmd.Parameters["@CategoryName"].Value = p.CategoryName;
     cmd.Parameters.Add("@ProductId", SqlDbType.Int, 4);
     cmd.Parameters["@ProductId"].Value = p.ProductId;
     cmd.Parameters["@ProductId"].Direction = ParameterDirection.Output;
     try
     {
         con.Open();
         int rows = cmd.ExecuteNonQuery();                       //create the new product in DB
         p.ProductId = (int)cmd.Parameters["@ProductId"].Value;  //set the returned ProductId in the SqlProduct object
         displayP.ProductAdded2DB(p);                            //update corresponding Product ProductId using SqlProduct
     }
     catch (SqlException ex)
     {
         errorMessage = "Add SQL error, " + ex.Message;
         hasError = true;
     }
     catch (Exception ex)
     {
         errorMessage = "ADD error, " + ex.Message;
         hasError = true;
     }
     finally
     {
         con.Close();
     }
     return !hasError;
 } //AddProduct()
  */
 public bool AddProduct(Product displayP)
 {
     hasError = false;
     try
     {
         SqlProduct p = new SqlProduct(displayP);
         LinqDataContext dc = new LinqDataContext();
         int? newProductId = 0;
         dc.AddProduct(p.CategoryName, p.ModelNumber, p.ModelName, p.UnitCost, p.Description, ref newProductId);
         p.ProductId = (int)newProductId;
         displayP.ProductAdded2DB(p);    //update corresponding Product ProductId using SqlProduct
     }
     catch (Exception ex)
     {
         errorMessage = "Add error, " + ex.Message;
         hasError = true;
     }
     return !hasError;
 }
Exemplo n.º 2
0
        }// DeleteProduct()

/*
 *      public bool AddProduct(Product displayP)
 *      {
 *          SqlProduct p = new SqlProduct(displayP);
 *          hasError = false;
 *          SqlConnection con = new SqlConnection(conString);
 *          SqlCommand cmd = new SqlCommand("AddProduct", con);
 *          cmd.CommandType = CommandType.StoredProcedure;
 *          cmd.Parameters.Add("@ModelNumber", SqlDbType.VarChar, prodStringLen);
 *          cmd.Parameters["@ModelNumber"].Value = p.ModelNumber;
 *          cmd.Parameters.Add("@ModelName", SqlDbType.VarChar, prodStringLen);
 *          cmd.Parameters["@ModelName"].Value = p.ModelName;
 *          cmd.Parameters.Add("@UnitCost", SqlDbType.Decimal);
 *          cmd.Parameters["@UnitCost"].Value = p.UnitCost;
 *          cmd.Parameters.Add("@Description", SqlDbType.VarChar, 200);
 *          cmd.Parameters["@Description"].Value = p.Description;
 *          if (p.Description==null)  cmd.Parameters["@Description"].Value = DBNull.Value;
 *          cmd.Parameters.Add("@CategoryName", SqlDbType.VarChar, prodStringLen);
 *          cmd.Parameters["@CategoryName"].Value = p.CategoryName;
 *          cmd.Parameters.Add("@ProductId", SqlDbType.Int, 4);
 *          cmd.Parameters["@ProductId"].Value = p.ProductId;
 *          cmd.Parameters["@ProductId"].Direction = ParameterDirection.Output;
 *          try
 *          {
 *              con.Open();
 *              int rows = cmd.ExecuteNonQuery();                       //create the new product in DB
 *              p.ProductId = (int)cmd.Parameters["@ProductId"].Value;  //set the returned ProductId in the SqlProduct object
 *              displayP.ProductAdded2DB(p);                            //update corresponding Product ProductId using SqlProduct
 *          }
 *          catch (SqlException ex)
 *          {
 *              errorMessage = "Add SQL error, " + ex.Message;
 *              hasError = true;
 *          }
 *          catch (Exception ex)
 *          {
 *              errorMessage = "ADD error, " + ex.Message;
 *              hasError = true;
 *          }
 *          finally
 *          {
 *              con.Close();
 *          }
 *          return !hasError;
 *      } //AddProduct()
 */
        public bool AddProduct(Product displayP)
        {
            hasError = false;
            try
            {
                SqlProduct      p            = new SqlProduct(displayP);
                LinqDataContext dc           = new LinqDataContext();
                int?            newProductId = 0;
                dc.AddProduct(p.CategoryName, p.ModelNumber, p.ModelName, p.UnitCost, p.Description, ref newProductId);
                p.ProductId = (int)newProductId;
                displayP.ProductAdded2DB(p);    //update corresponding Product ProductId using SqlProduct
            }
            catch (Exception ex)
            {
                errorMessage = "Add error, " + ex.Message;
                hasError     = true;
            }
            return(!hasError);
        } //AddProduct()