TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
ASP. Net CRUD OPERATION USING 3TIER METHOD
sreepathy s prabhu
May 12, 2015
3.5
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
In this blog we will learn about ASP. Net Crud Operation using 3TIER Method.
PRESENTATION Layer
#region Used namespaces
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Data.SqlClient;
using
WebWareHouseBLL;#endregion Used namespaces
namespace
WebWareHouse {
public
partial
class
ShoppingCart: System.Web.UI.Page {
int
custId;#region On Page Load
protected
void
Page_Load(
object
sender, EventArgs e) {
if
(!IsPostBack) {
BindData();
}
}#endregion On Page Load
#region BindData
public
DataSet BindData() {
custId = Convert.ToInt32(Session[
"CustomerId"
]);
ShopingCartBLL cartObj =
new
ShopingCartBLL();
cartObj.UserId = custId;
DataSet dsitem = cartObj.GetAllCartitems();
gdShoppingCart.DataSource = dsitem.Tables[0];
gdShoppingCart.DataBind();
return
dsitem;
}#endregion BindData
protected
void
gdShoppingCart_SelectedIndexChanged(
object
sender, EventArgs e) {}
protected
void
btnCheckout_Click(
object
sender, EventArgs e) {}
#region Row Updating
protected
void
gdShoppingCart_RowUpdating(
object
sender, GridViewUpdateEventArgs e) {
bool
result;
custId = Convert.ToInt32(Session[
"CustomerId"
]);
ShopingCartBLL cartObj =
new
ShopingCartBLL();
cartObj.BookId = Convert.ToInt32(gdShoppingCart.Rows[e.RowIndex].Cells[0].Text);
cartObj.Title = gdShoppingCart.Rows[e.RowIndex].Cells[1].Text;
cartObj.Price = Convert.ToDouble(gdShoppingCart.Rows[e.RowIndex].Cells[2].Text);
TextBox txtQuantity = (TextBox)(gdShoppingCart.Rows[e.RowIndex].Cells[3].Controls[0]);
cartObj.Quantity = Convert.ToInt32(txtQuantity.Text);
//cartObj.Amount = Convert.ToDouble(gdShoppingCart.Rows[e.RowIndex].Cells[4].Text);
cartObj.UserId = custId;
//replace it with session or pass url
result = cartObj.UpdateAnItem();
if
(result) {
string
script =
"<script>alert('UpDated')</script>"
;
Page.ClientScript.RegisterClientScriptBlock(
this
.GetType(),
"UpDated"
, script);
gdShoppingCart.EditIndex = -1;
BindData();
}
else
{
string
script =
"<script>alert('UpDated Failed')</script>"
;
Page.ClientScript.RegisterClientScriptBlock(
this
.GetType(),
"UpDated"
, script);
}
}#endregion Row Updating
#region Row Editing
protected
void
gdShoppingCart_RowEditing(
object
sender, GridViewEditEventArgs e) {
gdShoppingCart.EditIndex = e.NewEditIndex;
BindData();
}#endregion Row Editing
#region Row Deleting
protected
void
gdShoppingCart_RowDeleting(
object
sender, GridViewDeleteEventArgs e) {
bool
result;
custId = Convert.ToInt32(Session[
"CustomerId"
]);
ShopingCartBLL cartObj =
new
ShopingCartBLL();
string
delete = gdShoppingCart.Rows[e.RowIndex].Cells[0].Text;
cartObj.BookId = Convert.ToInt32(delete);
cartObj.UserId = custId;
result = cartObj.RemoveAnItem();
if
(result) {
string
script =
"<script>alert('Deleted')</script>"
;
Page.ClientScript.RegisterClientScriptBlock(
this
.GetType(),
"deleted"
, script);
gdShoppingCart.EditIndex = -1;
BindData();
}
else
{
string
script =
"<script>alert('Deletion Failed')</script>"
;
Page.ClientScript.RegisterClientScriptBlock(
this
.GetType(),
"failed"
, script);
}
}#endregion Row Deleting
#region Row Cancelling
protected
void
gdShoppingCart_RowCancelingEdit(
object
sender, GridViewCancelEditEventArgs e) {
gdShoppingCart.EditIndex = -1;
BindData();
}#endregion ROw Cancelling
#region Row DataBound
protected
void
gdShoppingCart_RowDataBound(
object
sender, GridViewRowEventArgs e) {}
decimal
total = 0;
decimal
shippingpercentage;
protected
void
gdShoppingCart_RowDataBound1(
object
sender, GridViewRowEventArgs e) {
if
(e.Row.RowType == DataControlRowType.DataRow) {
Label lblprice = (Label) e.Row.FindControl(
"lblAmount"
);
decimal
price =
decimal
.Parse(lblprice.Text);
total += price;
}
if
(e.Row.RowType == DataControlRowType.Footer) {
TextBox lbltotalprice = (TextBox) e.Row.FindControl(
"txtTotalAmount"
);
DropDownList ddlshipping = (DropDownList) e.Row.FindControl(
"ddlShipping"
);
DataSet dsCreditCard =
new
DataSet();
dsCreditCard.ReadXml(Server.MapPath(
"ShippingXml.xml"
));
ddlshipping.DataSource = dsCreditCard;
ddlshipping.DataTextField =
"text"
;
ddlshipping.DataValueField =
"value"
;
ddlshipping.DataBind();
shippingpercentage =
decimal
.Parse(ddlshipping.SelectedItem.Text);
shippingpercentage = shippingpercentage / 100;
total = total + total * Convert.ToDecimal(shippingpercentage);
lbltotalprice.Text = total.ToString();
TextBox txtShippingCharge = (TextBox) e.Row.FindControl(
"txtShipping"
);
shippingpercentage = shippingpercentage * 100;
txtShippingCharge.Text = Convert.ToString(shippingpercentage);
}
}#endregion Row DataBound
protected
void
gdShoppingCart_PageIndexChanging(
object
sender, GridViewPageEventArgs e) {
gdShoppingCart.PageIndex = e.NewPageIndex;
BindData();
}
protected
void
gdShoppingCart_SelectedIndexChanged1(
object
sender, EventArgs e) {
}
protected
void
txtShipping_DataBinding(
object
sender, EventArgs e) {
}
protected
void
btnCheckOut_Click1(
object
sender, EventArgs e) {
int
cusid;
double
amount;
cusid = Convert.ToInt32(Session[
"CustomerId"
].ToString());
TextBox txtBox = (TextBox)(gdShoppingCart.FooterRow.FindControl(
"txtTotalAmount"
));
amount =
double
.Parse(txtBox.Text);
Response.Redirect(
string
.Format(
"CreditCheck.aspx?id="
+ cusid +
"&amount="
+ amount));
}
}
}
BUSINESS LOGIC LAYER
#region Used Namespace
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Data;
using
System.Data.SqlClient;
using
WebWareHouseDAL;#endregion Used Namespace
namespace
WebWareHouseBLL {
public
class
ShopingCartBLL {#region Public Properties
/// <summary>
/// userid of user
/// </summary>
public
int
UserId {
get
;
set
;
}
/// <summary>
/// Book title of Selected Book
/// </summary>
public
string
Title {
get
;
set
;
}
/// <summary>
/// book id of selected
/// </summary>
public
int
BookId {
get
;
set
;
}
/// <summary>
/// price of selected book
/// </summary>
public
double
Price {
get
;
set
;
}
/// <summary>
/// number of quantity of book selected
/// </summary>
public
int
Quantity {
get
;
set
;
}
/// <summary>
/// Amount ,quantity of book times price of book
/// </summary>
public
double
Amount {
get
;
set
;
}#endregion Public Properties
#region Methods
#region Get All Cart Items
public
DataSet GetAllCartitems() {
ShopingCartDAL cartObj =
new
ShopingCartDAL();
cartObj.UserId = UserId;
DataSet dsitem = cartObj.GetAllCartitems();
return
dsitem;
}#endregion Get All Cart Items
#region Remove An Item
public
bool
RemoveAnItem() {
bool
flag;
ShopingCartDAL removeObj =
new
ShopingCartDAL();
removeObj.BookId = BookId;
removeObj.UserId = UserId;
DataSet dsitem =
new
DataSet();
flag = removeObj.RemoveAnItem();
return
flag;
}#endregion Remove An Item
#region Update An Item
public
bool
UpdateAnItem() {
bool
flag;
ShopingCartDAL updateObj =
new
ShopingCartDAL();
updateObj.BookId = BookId;
updateObj.UserId = UserId;
updateObj.Title = Title;
updateObj.Price = Price;
updateObj.Quantity = Quantity;
NewAmount();
Amount = NewAmount();
updateObj.Amount = Amount;
DataSet dsitem =
new
DataSet();
flag = updateObj.UpdateAnItem();
return
flag;
}#endregion Update An Item
#region Calculate New Amount
public
double
NewAmount() {
Amount = Quantity * Price;
return
Amount;
}#endregion Calculate New Amount
#region AddtoCartInsertion
public
bool
AddToCart() {
bool
flag;
ShopingCartDAL cartObj =
new
ShopingCartDAL();
cartObj.UserId = UserId;
cartObj.BookId = BookId;
cartObj.Price = Price;
cartObj.Quantity = Quantity;
cartObj.Amount = Amount;
flag = cartObj.AddToCart();
return
flag;
}#endregion AddtoCartInsertion
public
bool
IsExistingBook() {
bool
result;
ShopingCartDAL dalObj =
new
ShopingCartDAL();
dalObj.BookId = BookId;
dalObj.UserId = UserId;
result = dalObj.CheckExistingBoolInCart();
return
result;
}
public
bool
DeleteCart() {
bool
flag;
ShopingCartDAL dalobj =
new
ShopingCartDAL();
dalobj.UserId = UserId;
flag = dalobj.DeleteCart();
return
flag;
}
#endregion Methods
}
}
DATA ACESS LAYER
#region Uesd Namespace
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Data;
using
System.Data.SqlClient;#endregion Used Namespace
namespace
WebWareHouseDAL {
public
class
ShopingCartDAL {#region Public Properties
/// <summary>
/// userid of user
/// </summary>
public
int
UserId {
get
;
set
;
}
/// <summary>
/// Book title of Selected Book
/// </summary>
public
string
Title {
get
;
set
;
}
/// <summary>
/// book id of selected
/// </summary>
public
int
BookId {
get
;
set
;
}
/// <summary>
/// price of selected book
/// </summary>
public
double
Price {
get
;
set
;
}
/// <summary>
/// number of quantity of book selected
/// </summary>
public
int
Quantity {
get
;
set
;
}
/// <summary>
/// Amount ,quantity of book times price of book
/// </summary>
public
double
Amount {
get
;
set
;
}#endregion Public Properties#region Methods
#region GetAllCartItems
/// <summary>
/// To get all cart items of a user
/// </summary>
/// <returns>dataset of cart items</returns>
public
DataSet GetAllCartitems() {
SqlDataAdapter daitems;
SqlCommand cmd;
SqlConnection conObj =
null
;
DataSet dsitems =
null
;
try
{
//replace userid with session id or pass id in selectquery
conObj = WareHouseDBConnection.GetDbConnection();
conObj.Open();
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"
;
cmd =
new
SqlCommand(selectQuerty, conObj);
SqlParameter sqlparam1 = cmd.Parameters.AddWithValue(
"@custid"
, UserId);
daitems =
new
SqlDataAdapter(cmd);
dsitems =
new
DataSet();
daitems.Fill(dsitems);
}
catch
(Exception es) {
throw
es;
}
finally
{
conObj.Close();
}
return
dsitems;
}#endregion GetAllCartItems
#region Remove An Item
/// <summary>
/// To remove an item from users cart
/// </summary>
/// <returns>true/false</returns>
public
bool
RemoveAnItem() {
String removeString;
SqlCommand cmdremove;
int
rowsaffected;
SqlConnection conobj =
null
;
bool
result =
false
;
try
{
removeString =
"procDeleteACart"
;
conobj = WareHouseDBConnection.GetDbConnection();
cmdremove =
new
SqlCommand(removeString, conobj);
cmdremove.CommandType = CommandType.StoredProcedure;
conobj.Open();
cmdremove.Connection = conobj;
SqlParameter sqlparam1 = cmdremove.Parameters.AddWithValue(
"@userid"
, UserId);
SqlParameter sqlparam2 = cmdremove.Parameters.AddWithValue(
"@bookid"
, BookId);
rowsaffected = cmdremove.ExecuteNonQuery();
if
(rowsaffected > 0) {
result =
true
;
}
else
{
result =
false
;
}
}
catch
(Exception es) {
throw
es;
}
finally
{
conobj.Close();
}
return
result;
}#endregion Remove An Item
#region Update An Item
/// <summary>
/// To update number of Quantity of item And hence Amount
/// </summary>
/// <returns></returns>
public
bool
UpdateAnItem() {
string
updateQuery;
int
rowsAffected;
SqlCommand cmdupdate;
SqlConnection conobj =
null
;
bool
result =
false
;
try
{
updateQuery =
"procUpdateACart"
;
conobj = WareHouseDBConnection.GetDbConnection();
cmdupdate =
new
SqlCommand(updateQuery, conobj);
cmdupdate.CommandType = CommandType.StoredProcedure;
conobj.Open();
cmdupdate.Connection = conobj;
SqlParameter sqlparam1 = cmdupdate.Parameters.AddWithValue(
"@quantity"
, Quantity);
SqlParameter sqlparam2 = cmdupdate.Parameters.AddWithValue(
"@userid"
, UserId);
SqlParameter sqlparam3 = cmdupdate.Parameters.AddWithValue(
"@bookid"
, BookId);
SqlParameter sqlparam4 = cmdupdate.Parameters.AddWithValue(
"@amount"
, Amount);
rowsAffected = cmdupdate.ExecuteNonQuery();
if
(rowsAffected > 0) {
result =
true
;
}
else
{
result =
false
;
}
}
catch
(Exception es) {
throw
es;
}
finally
{
conobj.Close();
}
return
result;
}
#endregion Update An Item
#region AddtoCartInsertion
public
bool
AddToCart() {
SqlCommand cmd;
string
insertQuery;
int
numRows;
bool
result;
SqlConnection con =
null
;
try
{
con = WareHouseDBConnection.GetDbConnection();
con.Open();
insertQuery =
"procInsertToCart"
;
cmd =
new
SqlCommand(insertQuery, con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter sqlParam1 = cmd.Parameters.AddWithValue(
"@userid"
, UserId);
SqlParameter sqlParam2 = cmd.Parameters.AddWithValue(
"@bookid"
, BookId);
SqlParameter sqlParam3 = cmd.Parameters.AddWithValue(
"@price"
, Price);
SqlParameter sqlParam4 = cmd.Parameters.AddWithValue(
"@quantity"
, Quantity);
SqlParameter sqlparam5 = cmd.Parameters.AddWithValue(
"@amount"
, Amount);
numRows = cmd.ExecuteNonQuery();
if
(numRows > 0) {
result =
true
;
}
else
{
result =
false
;
}
}
catch
(SqlException ex) {
throw
ex;
}
finally
{
con.Close();
}
return
result;
}
#endregion AddtoCartInsertion
public
bool
CheckExistingBoolInCart() {
string
selectQuery;
SqlCommand cmdSelect;
SqlDataReader drSelect;
SqlConnection cObj =
null
;
int
count = 0;
bool
result =
false
;
try
{
selectQuery =
"SELECT car_bookid from dbo.web_cartmanagement where car_userid=@idpass And car_bookid=@bookidpass"
;
cObj = WareHouseDBConnection.GetDbConnection();
cObj.Open();
cmdSelect =
new
SqlCommand(selectQuery, cObj);
SqlParameter sqlparam0 = cmdSelect.Parameters.AddWithValue(
"@idpass"
, UserId);
SqlParameter sqlparam1 = cmdSelect.Parameters.AddWithValue(
"@bookidpass"
, BookId);
drSelect = cmdSelect.ExecuteReader();
while
(drSelect.Read()) {
count = count + 1;
}
if
(count > 0) {
result =
true
;
}
else
{
result =
false
;
}
}
catch
(SqlException ex) {
throw
ex;
}
finally
{
cObj.Close();
}
return
result;
}
public
bool
DeleteCart() {
string
deleteQuery;
SqlCommand cmdSelect;
SqlDataReader reader;
bool
result;
SqlConnection cObj =
null
;
try
{
deleteQuery =
"delete from web_cartmanagement where car_userid=@id"
;
cObj = WareHouseDBConnection.GetDbConnection();
cObj.Open();
cmdSelect =
new
SqlCommand(deleteQuery, cObj);
SqlParameter sqlparam = cmdSelect.Parameters.AddWithValue(
"@id"
, UserId);
reader = cmdSelect.ExecuteReader();
if
(reader.Read()) {
result =
true
;
}
else
{
result =
false
;
}
}
catch
(SqlException ex) {
throw
ex;
}
finally
{
cObj.Close();
}
return
result;
}
#endregion Methods
}
}
ASP. Net CRUD OPERATION USING 3TIER METHOD
Next Recommended Reading
CRUD Operation Using 3-Tier Architecture In ASP.NET With jQuery Validation