185522x

Untitled

Aug 3rd, 2019
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5.  
  6.  
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Configuration;
  10.  
  11. /// <summary>
  12. /// Summary description for Product
  13. /// </summary>
  14. public class Product
  15. {
  16.  
  17.     //Private string _connStr = Properties.Settings.Default.DBConnStr;
  18.  
  19.  
  20.     //System.Configuration.ConnectionStringSettings _connStr;
  21.     public static string m = HttpContext.Current.Server.MapPath("~/App_Data");
  22.  
  23.     string _connStr = @"Data Source=(LocalDB)\mssqllocaldb;AttachDbFilename=" + m + "\\StoreDB.mdf;Integrated Security=True";
  24.  
  25.     private string _prodID = null;
  26.     private string _prodName = string.Empty;
  27.     private string _prodDesc = ""; // this is another way to specify empty string
  28.     private decimal _unitPrice = 0;
  29.     private string _prodImage = "";
  30.     private int _stockLevel = 0;
  31.     private string _prodType = string.Empty;
  32.  
  33.  
  34.     // Default constructor
  35.     public Product()
  36.     {
  37.     }
  38.  
  39.  
  40.     // Constructor that take in all data required to build a Product object
  41.     public Product(string prodID, string prodName, string prodDesc,
  42.         decimal unitPrice, string prodImage, int stockLevel, string prodType)
  43.     {
  44.         _prodID = prodID;
  45.         _prodName = prodName;
  46.         _prodDesc = prodDesc;
  47.         _unitPrice = unitPrice;
  48.         _prodImage = prodImage;
  49.         _stockLevel = stockLevel;
  50.         _prodType = prodType;
  51.     }
  52.  
  53.  
  54.     // Constructor that take in all except product ID
  55.     public Product(string prodName, string prodDesc,
  56.         decimal unitPrice, string prodImage, int stockLevel, string prodType)
  57.         : this(null, prodName, prodDesc, unitPrice, prodImage, stockLevel, prodType)
  58.     {
  59.     }
  60.  
  61.  
  62.     // Constructor that take in only Product ID. The other attributes will be set to 0 or empty.
  63.     public Product(string prodID)
  64.         : this(prodID, "", "", 0, "", 0, "")
  65.     {
  66.     }
  67.  
  68.     // Get/Set the attributes of the Product object.
  69.     // Note the attribute name (e.g. Product_ID) is same as the actual database field name.
  70.     // This is for ease of referencing.
  71.     public string Product_ID
  72.     {
  73.         get { return _prodID; }
  74.         set { _prodID = value; }
  75.     }
  76.     public string Product_Name
  77.     {
  78.         get { return _prodName; }
  79.         set { _prodName = value; }
  80.     }
  81.     public string Product_Desc
  82.     {
  83.         get { return _prodDesc; }
  84.         set { _prodDesc = value; }
  85.     }
  86.     public decimal Unit_Price
  87.     {
  88.         get { return _unitPrice; }
  89.         set { _unitPrice = value; }
  90.     }
  91.     public string Product_Image
  92.     {
  93.         get { return _prodImage; }
  94.         set { _prodImage = value; }
  95.     }
  96.     public int QuantityInStock
  97.     {
  98.         get { return _stockLevel; }
  99.         set { _stockLevel = value; }
  100.     }
  101.  
  102.     public string Product_Type
  103.     {
  104.         get { return _prodType; }
  105.         set { _prodType = value; }
  106.     }
  107.  
  108.     //Below as the Class methods for some DB operations.
  109.     public Product getProduct(string prodID)
  110.     {
  111.  
  112.         Product prodDetail = null;
  113.  
  114.         string prod_Name, prod_Desc, Prod_Image, prod_Type;
  115.         decimal unit_Price;
  116.         int stock_Level;
  117.  
  118.  
  119.         string queryStr = "SELECT * FROM Products WHERE Product_ID = @ProdID";
  120.  
  121.  
  122.         SqlConnection conn = new SqlConnection(_connStr);
  123.         SqlCommand cmd = new SqlCommand(queryStr, conn);
  124.         cmd.Parameters.AddWithValue("@ProdID", prodID);
  125.  
  126.         conn.Open();
  127.         SqlDataReader dr = cmd.ExecuteReader();
  128.  
  129.         if (dr.Read())
  130.         {
  131.             prod_Name = dr["Product_Name"].ToString();
  132.             prod_Desc = dr["Product_Desc"].ToString();
  133.             Prod_Image = dr["Product_Image"].ToString();
  134.             unit_Price = decimal.Parse(dr["Unit_Price"].ToString());
  135.             stock_Level = int.Parse(dr["QuantityInStock"].ToString());
  136.             prod_Type = dr["Product_Type"].ToString();
  137.  
  138.  
  139.             prodDetail = new Product(prodID, prod_Name, prod_Desc, unit_Price, Prod_Image, stock_Level, prod_Type);
  140.         }
  141.         else
  142.         {
  143.             prodDetail = null;
  144.         }
  145.         conn.Close();
  146.         dr.Close();
  147.         dr.Dispose();
  148.  
  149.         return prodDetail;
  150.     }
  151.  
  152.  
  153.     public List<Product> getProductAll()
  154.     {
  155.         List<Product> prodList = new List<Product>();
  156.  
  157.  
  158.         string prod_Name, prod_Desc, Prod_Image, prod_ID, prod_Type;
  159.         decimal unit_Price;
  160.         int stock_Level;
  161.  
  162.  
  163.         string queryStr = "SELECT * FROM Products Order By Product_ID";
  164.  
  165.  
  166.         SqlConnection conn = new SqlConnection(_connStr);
  167.         SqlCommand cmd = new SqlCommand(queryStr, conn);
  168.  
  169.         conn.Open();
  170.         SqlDataReader dr = cmd.ExecuteReader();
  171.  
  172.  
  173.         while (dr.Read())
  174.         {
  175.             prod_ID = dr["Product_ID"].ToString();
  176.             prod_Name = dr["Product_Name"].ToString();
  177.             prod_Desc = dr["Product_Desc"].ToString();
  178.             Prod_Image = dr["Product_Image"].ToString();
  179.             unit_Price = decimal.Parse(dr["Unit_Price"].ToString());
  180.             stock_Level = int.Parse(dr["QuantityInStock"].ToString());
  181.             prod_Type = dr["Product_Type"].ToString();
  182.             Product a = new Product(prod_ID, prod_Name, prod_Desc, unit_Price, Prod_Image, stock_Level, prod_Type);
  183.             prodList.Add(a);
  184.         }
  185.  
  186.  
  187.         conn.Close();
  188.         dr.Close();
  189.         dr.Dispose();
  190.  
  191.         return prodList;
  192.     }
  193.  
  194.  
  195.  
  196.  
  197.     public int ProductInsert()
  198.     {
  199.         string msg = null;
  200.         int result = 0;
  201.  
  202.  
  203.         string queryStr = "INSERT INTO Products(Product_ID,Product_Name, Product_Desc, Unit_Price, Product_Image,QuantityInStock, Product_Type)"
  204.         + "values (@Product_ID,@Product_Name, @Product_Desc, @Unit_Price, @Product_Image,@QuantityInStock,@Product_Type)";
  205.  
  206.  
  207.         SqlConnection conn = new SqlConnection(_connStr);
  208.         SqlCommand cmd = new SqlCommand(queryStr, conn);
  209.         cmd.Parameters.AddWithValue("@Product_ID", this.Product_ID);
  210.         cmd.Parameters.AddWithValue("@Product_Name", this.Product_Name);
  211.         cmd.Parameters.AddWithValue("@Product_Desc", this.Product_Desc);
  212.         cmd.Parameters.AddWithValue("@Unit_Price", this.Unit_Price);
  213.         cmd.Parameters.AddWithValue("@Product_Image", this.Product_Image);
  214.         cmd.Parameters.AddWithValue("@QuantityInStock", this.QuantityInStock);
  215.         cmd.Parameters.AddWithValue("@Product_Type", this.Product_Type);
  216.  
  217.         conn.Open();
  218.         result += cmd.ExecuteNonQuery(); // Returns no. of rows affected. Must be > 0
  219.         conn.Close();
  220.         return result;
  221.     }//end Insert
  222.  
  223.  
  224.     public int ProductDelete(string ID)
  225.     {
  226.         string queryStr = "DELETE FROM Products WHERE Product_ID=@ID";
  227.         SqlConnection conn = new SqlConnection(_connStr);
  228.         SqlCommand cmd = new SqlCommand(queryStr, conn);
  229.         cmd.Parameters.AddWithValue("@ID", ID);
  230.         conn.Open();
  231.         int nofRow = 0;
  232.         nofRow = cmd.ExecuteNonQuery();
  233.         conn.Close();
  234.         return nofRow;
  235.  
  236.     }//end Delete
  237.  
  238.  
  239.     public int ProductUpdate(string pId, string pName, decimal pUnitPrice)
  240.     {
  241.         string queryStr = "UPDATE Products SET" +
  242.         //" Product_ID = @productID, " +
  243.         " Product_Name = @productName, " +
  244.         " Unit_Price = @unitPrice " +
  245.         " WHERE Product_ID = @productID";
  246.  
  247.  
  248.         SqlConnection conn = new SqlConnection(_connStr);
  249.         SqlCommand cmd = new SqlCommand(queryStr, conn);
  250.         cmd.Parameters.AddWithValue("@productID", pId);
  251.         cmd.Parameters.AddWithValue("@productName", pName);
  252.         cmd.Parameters.AddWithValue("@unitPrice", pUnitPrice);
  253.  
  254.  
  255.         conn.Open();
  256.         int nofRow = 0;
  257.         nofRow = cmd.ExecuteNonQuery();
  258.  
  259.         conn.Close();
  260.  
  261.         return nofRow;
  262.     }//end Update
  263.  
  264. }
Advertisement
Add Comment
Please, Sign In to add comment