ASP. Net CRUD OPERATION USING 3TIER METHOD

PRESENTATION Layer


  1. #region Used namespaces  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Data;  
  9. using System.Data.SqlClient;  
  10. using WebWareHouseBLL;#endregion Used namespaces  
  11. namespace WebWareHouse {  
  12.     public partial class ShoppingCart: System.Web.UI.Page {  
  13.         int custId;#region On Page Load  
  14.         protected void Page_Load(object sender, EventArgs e) {  
  15.             if (!IsPostBack) {  
  16.                 BindData();  
  17.             }  
  18.         }#endregion On Page Load  
  19.         #region BindData  
  20.         public DataSet BindData() {  
  21.             custId = Convert.ToInt32(Session["CustomerId"]);  
  22.             ShopingCartBLL cartObj = new ShopingCartBLL();  
  23.             cartObj.UserId = custId;  
  24.             DataSet dsitem = cartObj.GetAllCartitems();  
  25.             gdShoppingCart.DataSource = dsitem.Tables[0];  
  26.             gdShoppingCart.DataBind();  
  27.             return dsitem;  
  28.         }#endregion BindData  
  29.         protected void gdShoppingCart_SelectedIndexChanged(object sender, EventArgs e) {}  
  30.         protected void btnCheckout_Click(object sender, EventArgs e) {}  
  31.         #region Row Updating  
  32.         protected void gdShoppingCart_RowUpdating(object sender, GridViewUpdateEventArgs e) {  
  33.             bool result;  
  34.             custId = Convert.ToInt32(Session["CustomerId"]);  
  35.             ShopingCartBLL cartObj = new ShopingCartBLL();  
  36.             cartObj.BookId = Convert.ToInt32(gdShoppingCart.Rows[e.RowIndex].Cells[0].Text);  
  37.             cartObj.Title = gdShoppingCart.Rows[e.RowIndex].Cells[1].Text;  
  38.             cartObj.Price = Convert.ToDouble(gdShoppingCart.Rows[e.RowIndex].Cells[2].Text);  
  39.             TextBox txtQuantity = (TextBox)(gdShoppingCart.Rows[e.RowIndex].Cells[3].Controls[0]);  
  40.             cartObj.Quantity = Convert.ToInt32(txtQuantity.Text);  
  41.             //cartObj.Amount = Convert.ToDouble(gdShoppingCart.Rows[e.RowIndex].Cells[4].Text);  
  42.             cartObj.UserId = custId; //replace it with session or pass url  
  43.             result = cartObj.UpdateAnItem();  
  44.             if (result) {  
  45.                 string script = "<script>alert('UpDated')</script>";  
  46.                 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UpDated", script);  
  47.                 gdShoppingCart.EditIndex = -1;  
  48.                 BindData();  
  49.             } else {  
  50.                 string script = "<script>alert('UpDated Failed')</script>";  
  51.                 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UpDated", script);  
  52.             }  
  53.         }#endregion Row Updating  
  54.         #region Row Editing  
  55.         protected void gdShoppingCart_RowEditing(object sender, GridViewEditEventArgs e) {  
  56.             gdShoppingCart.EditIndex = e.NewEditIndex;  
  57.             BindData();  
  58.         }#endregion Row Editing  
  59.  
  60.         #region Row Deleting  
  61.         protected void gdShoppingCart_RowDeleting(object sender, GridViewDeleteEventArgs e) {  
  62.             bool result;  
  63.             custId = Convert.ToInt32(Session["CustomerId"]);  
  64.             ShopingCartBLL cartObj = new ShopingCartBLL();  
  65.             string delete = gdShoppingCart.Rows[e.RowIndex].Cells[0].Text;  
  66.             cartObj.BookId = Convert.ToInt32(delete);  
  67.             cartObj.UserId = custId;  
  68.             result = cartObj.RemoveAnItem();  
  69.   
  70.             if (result) {  
  71.                 string script = "<script>alert('Deleted')</script>";  
  72.                 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "deleted", script);  
  73.                 gdShoppingCart.EditIndex = -1;  
  74.                 BindData();  
  75.             } else {  
  76.                 string script = "<script>alert('Deletion Failed')</script>";  
  77.                 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "failed", script);  
  78.             }  
  79.         }#endregion Row Deleting  
  80.  
  81.         #region Row Cancelling  
  82.         protected void gdShoppingCart_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {  
  83.             gdShoppingCart.EditIndex = -1;  
  84.             BindData();  
  85.         }#endregion ROw Cancelling  
  86.         #region Row DataBound  
  87.         protected void gdShoppingCart_RowDataBound(object sender, GridViewRowEventArgs e) {}  
  88.   
  89.         decimal total = 0;  
  90.         decimal shippingpercentage;  
  91.         protected void gdShoppingCart_RowDataBound1(object sender, GridViewRowEventArgs e) {  
  92.   
  93.             if (e.Row.RowType == DataControlRowType.DataRow) {  
  94.                 Label lblprice = (Label) e.Row.FindControl("lblAmount");  
  95.                 decimal price = decimal.Parse(lblprice.Text);  
  96.                 total += price;  
  97.             }  
  98.             if (e.Row.RowType == DataControlRowType.Footer) {  
  99.                 TextBox lbltotalprice = (TextBox) e.Row.FindControl("txtTotalAmount");  
  100.                 DropDownList ddlshipping = (DropDownList) e.Row.FindControl("ddlShipping");  
  101.                 DataSet dsCreditCard = new DataSet();  
  102.                 dsCreditCard.ReadXml(Server.MapPath("ShippingXml.xml"));  
  103.                 ddlshipping.DataSource = dsCreditCard;  
  104.                 ddlshipping.DataTextField = "text";  
  105.                 ddlshipping.DataValueField = "value";  
  106.                 ddlshipping.DataBind();  
  107.                 shippingpercentage = decimal.Parse(ddlshipping.SelectedItem.Text);  
  108.                 shippingpercentage = shippingpercentage / 100;  
  109.                 total = total + total * Convert.ToDecimal(shippingpercentage);  
  110.                 lbltotalprice.Text = total.ToString();  
  111.                 TextBox txtShippingCharge = (TextBox) e.Row.FindControl("txtShipping");  
  112.                 shippingpercentage = shippingpercentage * 100;  
  113.                 txtShippingCharge.Text = Convert.ToString(shippingpercentage);  
  114.             }  
  115.         }#endregion Row DataBound  
  116.         protected void gdShoppingCart_PageIndexChanging(object sender, GridViewPageEventArgs e) {  
  117.             gdShoppingCart.PageIndex = e.NewPageIndex;  
  118.             BindData();  
  119.         }  
  120.         protected void gdShoppingCart_SelectedIndexChanged1(object sender, EventArgs e) {  
  121.         }  
  122.         protected void txtShipping_DataBinding(object sender, EventArgs e) {  
  123.         }  
  124.         protected void btnCheckOut_Click1(object sender, EventArgs e) {  
  125.             int cusid;  
  126.             double amount;  
  127.             cusid = Convert.ToInt32(Session["CustomerId"].ToString());  
  128.             TextBox txtBox = (TextBox)(gdShoppingCart.FooterRow.FindControl("txtTotalAmount"));  
  129.             amount = double.Parse(txtBox.Text);  
  130.             Response.Redirect(string.Format("CreditCheck.aspx?id=" + cusid + "&amount=" + amount));  
  131.         }  
  132.     }  
  133. }  
BUSINESS LOGIC LAYER
  1. #region Used Namespace  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. using WebWareHouseDAL;#endregion Used Namespace  
  10. namespace WebWareHouseBLL {  
  11.     public class ShopingCartBLL {#region Public Properties  
  12.         /// <summary>  
  13.         /// userid of user  
  14.         /// </summary>  
  15.         public int UserId {  
  16.             get;  
  17.             set;  
  18.         }  
  19.         /// <summary>  
  20.         /// Book title of Selected Book  
  21.         /// </summary>  
  22.         public string Title {  
  23.             get;  
  24.             set;  
  25.         }  
  26.         /// <summary>  
  27.         /// book id of selected   
  28.         /// </summary>  
  29.         public int BookId {  
  30.             get;  
  31.             set;  
  32.         }  
  33.         /// <summary>  
  34.         /// price of selected book  
  35.         /// </summary>  
  36.         public double Price {  
  37.             get;  
  38.             set;  
  39.         }  
  40.         /// <summary>  
  41.         /// number of quantity of book selected  
  42.         /// </summary>  
  43.         public int Quantity {  
  44.             get;  
  45.             set;  
  46.         }  
  47.         /// <summary>  
  48.         /// Amount ,quantity of book times price of book  
  49.         /// </summary>  
  50.         public double Amount {  
  51.             get;  
  52.             set;  
  53.         }#endregion Public Properties  
  54.         #region Methods  
  55.         #region Get All Cart Items  
  56.         public DataSet GetAllCartitems() {  
  57.             ShopingCartDAL cartObj = new ShopingCartDAL();  
  58.             cartObj.UserId = UserId;  
  59.             DataSet dsitem = cartObj.GetAllCartitems();  
  60.             return dsitem;  
  61.         }#endregion Get All Cart Items  
  62.         #region Remove An Item  
  63.         public bool RemoveAnItem() {  
  64.             bool flag;  
  65.             ShopingCartDAL removeObj = new ShopingCartDAL();  
  66.             removeObj.BookId = BookId;  
  67.             removeObj.UserId = UserId;  
  68.             DataSet dsitem = new DataSet();  
  69.             flag = removeObj.RemoveAnItem();  
  70.             return flag;  
  71.         }#endregion Remove An Item  
  72.         #region Update An Item  
  73.         public bool UpdateAnItem() {  
  74.             bool flag;  
  75.             ShopingCartDAL updateObj = new ShopingCartDAL();  
  76.             updateObj.BookId = BookId;  
  77.             updateObj.UserId = UserId;  
  78.             updateObj.Title = Title;  
  79.             updateObj.Price = Price;  
  80.             updateObj.Quantity = Quantity;  
  81.             NewAmount();  
  82.             Amount = NewAmount();  
  83.             updateObj.Amount = Amount;  
  84.             DataSet dsitem = new DataSet();  
  85.             flag = updateObj.UpdateAnItem();  
  86.             return flag;  
  87.         }#endregion Update An Item  
  88.         #region Calculate New Amount  
  89.         public double NewAmount() {  
  90.             Amount = Quantity * Price;  
  91.             return Amount;  
  92.         }#endregion Calculate New Amount  
  93.         #region AddtoCartInsertion  
  94.         public bool AddToCart() {  
  95.             bool flag;  
  96.             ShopingCartDAL cartObj = new ShopingCartDAL();  
  97.             cartObj.UserId = UserId;  
  98.             cartObj.BookId = BookId;  
  99.             cartObj.Price = Price;  
  100.             cartObj.Quantity = Quantity;  
  101.             cartObj.Amount = Amount;  
  102.             flag = cartObj.AddToCart();  
  103.             return flag;  
  104.         }#endregion AddtoCartInsertion  
  105.         public bool IsExistingBook() {  
  106.             bool result;  
  107.             ShopingCartDAL dalObj = new ShopingCartDAL();  
  108.             dalObj.BookId = BookId;  
  109.             dalObj.UserId = UserId;  
  110.             result = dalObj.CheckExistingBoolInCart();  
  111.             return result;  
  112.         }  
  113.         public bool DeleteCart() {  
  114.             bool flag;  
  115.             ShopingCartDAL dalobj = new ShopingCartDAL();  
  116.             dalobj.UserId = UserId;  
  117.             flag = dalobj.DeleteCart();  
  118.             return flag;  
  119.         }  
  120.         #endregion Methods  
  121.     }  
  122. }  
DATA ACESS LAYER
  1. #region Uesd Namespace  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using System.Data;  
  8. using System.Data.SqlClient;#endregion Used Namespace  
  9. namespace WebWareHouseDAL {  
  10.     public class ShopingCartDAL {#region Public Properties  
  11.         /// <summary>  
  12.         /// userid of user  
  13.         /// </summary>  
  14.         public int UserId {  
  15.             get;  
  16.             set;  
  17.         }  
  18.         /// <summary>  
  19.         /// Book title of Selected Book  
  20.         /// </summary>  
  21.         public string Title {  
  22.             get;  
  23.             set;  
  24.         }  
  25.         /// <summary>  
  26.         /// book id of selected   
  27.         /// </summary>  
  28.         public int BookId {  
  29.             get;  
  30.             set;  
  31.         }  
  32.         /// <summary>  
  33.         /// price of selected book  
  34.         /// </summary>  
  35.         public double Price {  
  36.             get;  
  37.             set;  
  38.         }  
  39.         /// <summary>  
  40.         /// number of quantity of book selected  
  41.         /// </summary>  
  42.         public int Quantity {  
  43.             get;  
  44.             set;  
  45.         }  
  46.         /// <summary>  
  47.         /// Amount ,quantity of book times price of book  
  48.         /// </summary>  
  49.         public double Amount {  
  50.             get;  
  51.             set;  
  52.         }#endregion Public Properties#region Methods  
  53.  
  54.         #region GetAllCartItems  
  55.         /// <summary>  
  56.         /// To get all cart items of a user  
  57.         /// </summary>  
  58.         /// <returns>dataset of cart items</returns>  
  59.         public DataSet GetAllCartitems() {  
  60.             SqlDataAdapter daitems;  
  61.             SqlCommand cmd;  
  62.             SqlConnection conObj = null;  
  63.             DataSet dsitems = null;  
  64.             try { //replace userid with session id or pass id in selectquery  
  65.                 conObj = WareHouseDBConnection.GetDbConnection();  
  66.                 conObj.Open();  
  67.                 string selectQuerty = "select a.car_bookid,b.boo_title,a.car_price,a.car_quantity,a.car_amount,b.boo_id from web_cartmanagement a JOIN web_bookmanagement b ON a.car_bookid = b.boo_id where car_userid=@custid";  
  68.                 cmd = new SqlCommand(selectQuerty, conObj);  
  69.                 SqlParameter sqlparam1 = cmd.Parameters.AddWithValue("@custid", UserId);  
  70.                 daitems = new SqlDataAdapter(cmd);  
  71.                 dsitems = new DataSet();  
  72.                 daitems.Fill(dsitems);  
  73.             } catch (Exception es) {  
  74.                 throw es;  
  75.             } finally {  
  76.                 conObj.Close();  
  77.             }  
  78.             return dsitems;  
  79.         }#endregion GetAllCartItems  
  80.  
  81.         #region Remove An Item  
  82.         /// <summary>  
  83.         /// To remove an item from users cart  
  84.         /// </summary>  
  85.         /// <returns>true/false</returns>  
  86.         public bool RemoveAnItem() {  
  87.             String removeString;  
  88.             SqlCommand cmdremove;  
  89.             int rowsaffected;  
  90.             SqlConnection conobj = null;  
  91.             bool result = false;  
  92.             try {  
  93.                 removeString = "procDeleteACart";  
  94.                 conobj = WareHouseDBConnection.GetDbConnection();  
  95.                 cmdremove = new SqlCommand(removeString, conobj);  
  96.                 cmdremove.CommandType = CommandType.StoredProcedure;  
  97.                 conobj.Open();  
  98.                 cmdremove.Connection = conobj;  
  99.                 SqlParameter sqlparam1 = cmdremove.Parameters.AddWithValue("@userid", UserId);  
  100.                 SqlParameter sqlparam2 = cmdremove.Parameters.AddWithValue("@bookid", BookId);  
  101.                 rowsaffected = cmdremove.ExecuteNonQuery();  
  102.                 if (rowsaffected > 0) {  
  103.                     result = true;  
  104.                 } else {  
  105.                     result = false;  
  106.                 }  
  107.             } catch (Exception es) {  
  108.                 throw es;  
  109.             } finally {  
  110.                 conobj.Close();  
  111.             }  
  112.             return result;  
  113.         }#endregion Remove An Item  
  114.         #region Update An Item  
  115.         /// <summary>  
  116.         /// To update number of Quantity of item And hence Amount  
  117.         /// </summary>  
  118.         /// <returns></returns>  
  119.         public bool UpdateAnItem() {  
  120.             string updateQuery;  
  121.             int rowsAffected;  
  122.             SqlCommand cmdupdate;  
  123.             SqlConnection conobj = null;  
  124.             bool result = false;  
  125.             try {  
  126.                 updateQuery = "procUpdateACart";  
  127.                 conobj = WareHouseDBConnection.GetDbConnection();  
  128.                 cmdupdate = new SqlCommand(updateQuery, conobj);  
  129.                 cmdupdate.CommandType = CommandType.StoredProcedure;  
  130.                 conobj.Open();  
  131.                 cmdupdate.Connection = conobj;  
  132.                 SqlParameter sqlparam1 = cmdupdate.Parameters.AddWithValue("@quantity", Quantity);  
  133.                 SqlParameter sqlparam2 = cmdupdate.Parameters.AddWithValue("@userid", UserId);  
  134.                 SqlParameter sqlparam3 = cmdupdate.Parameters.AddWithValue("@bookid", BookId);  
  135.                 SqlParameter sqlparam4 = cmdupdate.Parameters.AddWithValue("@amount", Amount);  
  136.                 rowsAffected = cmdupdate.ExecuteNonQuery();  
  137.                 if (rowsAffected > 0) {  
  138.                     result = true;  
  139.                 } else {  
  140.                     result = false;  
  141.                 }  
  142.             } catch (Exception es) {  
  143.                 throw es;  
  144.             } finally {  
  145.                 conobj.Close();  
  146.             }  
  147.             return result;  
  148.         }  
  149.         #endregion Update An Item  
  150.  
  151.         #region AddtoCartInsertion  
  152.         public bool AddToCart() {  
  153.             SqlCommand cmd;  
  154.             string insertQuery;  
  155.             int numRows;  
  156.             bool result;  
  157.             SqlConnection con = null;  
  158.             try {  
  159.                 con = WareHouseDBConnection.GetDbConnection();  
  160.                 con.Open();  
  161.                 insertQuery = "procInsertToCart";  
  162.                 cmd = new SqlCommand(insertQuery, con);  
  163.                 cmd.CommandType = CommandType.StoredProcedure;  
  164.                 SqlParameter sqlParam1 = cmd.Parameters.AddWithValue("@userid", UserId);  
  165.                 SqlParameter sqlParam2 = cmd.Parameters.AddWithValue("@bookid", BookId);  
  166.                 SqlParameter sqlParam3 = cmd.Parameters.AddWithValue("@price", Price);  
  167.                 SqlParameter sqlParam4 = cmd.Parameters.AddWithValue("@quantity", Quantity);  
  168.                 SqlParameter sqlparam5 = cmd.Parameters.AddWithValue("@amount", Amount);  
  169.                 numRows = cmd.ExecuteNonQuery();  
  170.                 if (numRows > 0) {  
  171.                     result = true;  
  172.                 } else {  
  173.                     result = false;  
  174.                 }  
  175.             } catch (SqlException ex) {  
  176.                 throw ex;  
  177.             } finally {  
  178.                 con.Close();  
  179.             }  
  180.             return result;  
  181.         }  
  182.         #endregion AddtoCartInsertion  
  183.         public bool CheckExistingBoolInCart() {  
  184.             string selectQuery;  
  185.             SqlCommand cmdSelect;  
  186.             SqlDataReader drSelect;  
  187.             SqlConnection cObj = null;  
  188.             int count = 0;  
  189.             bool result = false;  
  190.             try {  
  191.                 selectQuery = "SELECT car_bookid from dbo.web_cartmanagement where car_userid=@idpass And car_bookid=@bookidpass";  
  192.                 cObj = WareHouseDBConnection.GetDbConnection();  
  193.                 cObj.Open();  
  194.                 cmdSelect = new SqlCommand(selectQuery, cObj);  
  195.                 SqlParameter sqlparam0 = cmdSelect.Parameters.AddWithValue("@idpass", UserId);  
  196.                 SqlParameter sqlparam1 = cmdSelect.Parameters.AddWithValue("@bookidpass", BookId);  
  197.                 drSelect = cmdSelect.ExecuteReader();  
  198.                 while (drSelect.Read()) {  
  199.                     count = count + 1;  
  200.                 }  
  201.                 if (count > 0) {  
  202.                     result = true;  
  203.                 } else {  
  204.                     result = false;  
  205.                 }  
  206.             } catch (SqlException ex) {  
  207.                 throw ex;  
  208.             } finally {  
  209.                 cObj.Close();  
  210.             }  
  211.             return result;  
  212.         }  
  213.         public bool DeleteCart() {  
  214.             string deleteQuery;  
  215.             SqlCommand cmdSelect;  
  216.             SqlDataReader reader;  
  217.             bool result;  
  218.             SqlConnection cObj = null;  
  219.             try {  
  220.                 deleteQuery = "delete from web_cartmanagement where car_userid=@id";  
  221.                 cObj = WareHouseDBConnection.GetDbConnection();  
  222.                 cObj.Open();  
  223.                 cmdSelect = new SqlCommand(deleteQuery, cObj);  
  224.                 SqlParameter sqlparam = cmdSelect.Parameters.AddWithValue("@id", UserId);  
  225.                 reader = cmdSelect.ExecuteReader();  
  226.                 if (reader.Read()) {  
  227.                     result = true;  
  228.                 } else {  
  229.                     result = false;  
  230.                 }  
  231.             } catch (SqlException ex) {  
  232.                 throw ex;  
  233.             } finally {  
  234.                 cObj.Close();  
  235.             }  
  236.             return result;  
  237.         }  
  238.         #endregion Methods  
  239.     }  
  240. }