Example #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;
 }
Example #2
0
 //Creating a new product in the DB assigns the ProductId
 //Update the ProductId from the value in the corresponding SqlProduct
 public void ProductAdded2DB(SqlProduct sqlProduct)
 {
     this._productId = sqlProduct.ProductId;
 }
Example #3
0
 /*
 private string GetStringOrNull(SqlDataReader reader, string columnName)
 {
     return reader.IsDBNull(reader.GetOrdinal(columnName)) ? "" : (string)reader[columnName];
 }
 */
 /*
 private const int prodStringLen = 50;
 public bool UpdateProduct(Product displayP)
 {
     SqlProduct p = new SqlProduct( displayP);
     hasError = false;
     SqlConnection con = new SqlConnection(conString);
     SqlCommand cmd = new SqlCommand("UpdateProduct", con);
     cmd.CommandType = CommandType.StoredProcedure;
     cmd.Parameters.Add("@ProductId", SqlDbType.Int, 4);
     cmd.Parameters["@ProductId"].Value = p.ProductId;
     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;
     cmd.Parameters.Add("@CategoryName", SqlDbType.VarChar, prodStringLen);
     cmd.Parameters["@CategoryName"].Value = p.CategoryName;
     int rows = 0;
     try
     {
         con.Open();
         rows = cmd.ExecuteNonQuery();
     }
     catch (SqlException ex)
     {
         errorMessage = "Update SQL error, " + ex.Message;
         hasError = true;
     }
     catch (Exception ex)
     {
         errorMessage = "Update error, " + ex.Message;
         hasError = true;
     }
     finally
     {
         con.Close();
     }
     return (!hasError);
 } //UpdateProduct()
 */
 public bool UpdateProduct(Product displayP)
 {
     try
     {
         SqlProduct p = new SqlProduct(displayP);
         LinqDataContext dc = new LinqDataContext();
         dc.UpdateProduct(p.ProductId, p.CategoryName, p.ModelNumber, p.ModelName, p.UnitCost, p.Description);
     }
     catch (Exception ex)
     {
         errorMessage = "Update error, " + ex.Message;
         hasError = true;
     }
     return (!hasError);
 }
Example #4
0
 //Creating a new product in the DB assigns the ProductId
 //Update the ProductId from the value in the corresponding SqlProduct
 public void ProductAdded2DB(SqlProduct sqlProduct)
 {
     this._productId = sqlProduct.ProductId;
 }