<%@ Page debug=true Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<html>
<head>
<title>Shopping Cart</title>
<script runat="server">
Dim objDT As System.Data.DataTable
Dim objDR As System.Data.DataRow
Private Sub Page_Load(s As Object, e As EventArgs)
If Not IsPostBack Then
makeCart()
End If
End Sub
Function makeCart()
objDT = New System.Data.DataTable("Cart")
objDT.Columns.Add("ID", GetType(Integer))
objDT.Columns("ID").AutoIncrement = True
objDT.Columns("ID").AutoIncrementSeed = 1
objDT.Columns.Add("Quantity", GetType(Integer))
objDT.Columns.Add("Product", GetType(String))
objDT.Columns.Add("Cost", GetType(Decimal))
Session("Cart") = objDT
End Function
Sub AddToCart(s As Object, e As EventArgs)
objDT = Session("Cart")
Dim Product = ddlProducts.SelectedItem.Text
Dim blnMatch As Boolean = False
For Each objDR In objDT.Rows
If objDR("Product") = Product Then
objDR("Quantity") += txtQuantity.Text
blnMatch = True
Exit For
End If
Next
If Not blnMatch Then
objDR = objDT.NewRow
objDR("Quantity") = txtQuantity.Text
objDR("Product") = ddlProducts.SelectedItem.Text
objDR("Cost") = Decimal.Parse(ddlProducts.SelectedItem.Value)
objDT.Rows.Add(objDR)
End If
Session("Cart") = objDT
dg.DataSource = objDT
dg.DataBind()
lblTotal.Text = "$" & GetItemTotal()
End Sub
Function GetItemTotal() As Decimal
Dim intCounter As Integer
Dim decRunningTotal As Decimal
For intCounter = 0 To objDT.Rows.Count - 1
objDR = objDT.Rows(intCounter)
decRunningTotal += (objDR("Cost") * objDR("Quantity"))
Next
Return decRunningTotal
End Function
Sub Delete_Item(s As Object, e As DataGridCommandEventArgs)
objDT = Session("Cart")
objDT.Rows(e.Item.ItemIndex).Delete()
Session("Cart") = objDT
dg.DataSource = objDT
dg.DataBind()
lblTotal.Text = "$" & GetItemTotal()
End Sub
</script>
</head>
<body>
<form runat="server">
Product:<br>
<asp:DropDownList id=ddlProducts runat="server">
<asp:ListItem Value="4.99">Socks</asp:ListItem>
<asp:ListItem Value="34.99">Pants</asp:ListItem>
<asp:ListItem Value="14.99">Shirt</asp:ListItem>
<asp:ListItem Value="12.99">Hat</asp:ListItem>
</asp:DropDownList><br>
Quantity:<br>
<asp:textbox id="txtQuantity" runat="server" /><br><br>
<asp:Button id=btnAdd runat="server" Text="Add To Cart" onClick="AddToCart" /><br><br>
<asp:DataGrid id=dg runat="server" ondeletecommand="Delete_Item">
<columns>
<asp:buttoncolumn buttontype="LinkButton" commandname="Delete" text="Remove Item" />
</columns>
</asp:DataGrid>
<br><br>
Total:
<asp:Label id=lblTotal runat="server" />
</form>
</body>
</html>
-----------------------------------------------------------------------------------------------------------
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="EcommerceApp._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager ID="ScriptManager1" ScriptMode="Release" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table width="80%">
<tr>
<td align="left" valign="top" style="width: 70%">
<asp:DataList Width="100%" ID="dlistProduct" runat="server" RepeatColumns="2" DataKeyField="ID"
OnItemCommand="dlistProduct_ItemCommand" OnItemDataBound="dlistProduct_ItemDataBound">
<ItemTemplate>
<table>
<tr>
<td>
<img id="imgProduct" src='<%#Eval("ProductImage")%>' height="100px" width="100px" />
<asp:HiddenField ID="hndID" runat="server" Value='<%#Eval("ID")%>' />
</td>
<td>
<table>
<tr>
<td>
<b>Name:</b>
<%#Eval("ProductName")%>
</td>
</tr>
<tr>
<td>
<b>Brand:</b>
<asp:HiddenField ID="hndBrandID" runat="server" Value='<%#Eval("BrandID")%>' />
<asp:Label ID="lblBrandName" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<b>Cost:</b>
<%#Eval("Cost")%>
INR
</td>
</tr>
<tr>
<td>
<asp:HiddenField ID="hndAU" runat="server" Value='<%#Eval("Availableunit")%>' />
<table>
<tr>
<td>
<b>Size:</b>
</td>
<td>
<asp:RadioButtonList ID="rdlist" RepeatDirection="Vertical" runat="server">
</asp:RadioButtonList>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnAddtocart" runat="server" CommandArgument='<%#Eval("ID")%>' CommandName="Addtocart"
Text="Add To Cart" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</td>
<td align="right" valign="top" style="width: 30%">
<table id="tblCart" runat="server" visible="false" border="1" style="border-color: #336666;
border-bottom-style: solid">
<tr>
<td align="left" valign="top">
<asp:DataGrid ID="dg" DataKeyField="ID" runat="server" AutoGenerateColumns="false"
OnDeleteCommand="Delete_Item" BackColor="White" CellPadding="2" GridLines="Horizontal">
<Columns>
<asp:BoundColumn DataField="Product" HeaderText="Product"></asp:BoundColumn>
<asp:BoundColumn DataField="Size" HeaderText="Size"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Quantity">
<ItemTemplate>
<asp:Label ID="lblQty" runat="server" Text='<%#Eval("Quantity")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtQty" runat="server" Text='<%#Eval("Quantity")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Cost" HeaderText="Cost"></asp:BoundColumn>
<asp:ButtonColumn ButtonType="LinkButton" CommandName="Delete" Text="Remove" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<ItemStyle ForeColor="#333333" BackColor="White" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" Mode="NumericPages" />
<SelectedItemStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>
</td>
</tr>
<tr>
<td align="left" valign="top">
Total:
<asp:Label ID="lblTotal" runat="server" />
</td>
</tr>
<tr>
<td align="center" valign="top">
<asp:Button ID="btnCheckout" runat="server" Text="CheckOut" PostBackUrl="OrderDetail.aspx" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
-----------------------------------------------------------------------------------------------------
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 EcommerceApp.BLayer;
namespace EcommerceApp
{
public partial class _Default : System.Web.UI.Page
{
#region Event
Common cm = new Common();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowProduct();
List<Shoppingcart> cart = Session["Cart"] as List<Shoppingcart>;
if (cart != null && cart.Count() > 0)
{
dg.DataSource = cart;
dg.DataBind();
lblTotal.Text = "$" + GetItemTotal();
}
}
}
public void Delete_Item(object s, DataGridCommandEventArgs e)
{
List<Shoppingcart> cart = Session["Cart"] as List<Shoppingcart>;
cart.Remove(cart[e.Item.ItemIndex]);
Session["Cart"] = cart;
dg.DataSource = cart;
dg.DataBind();
lblTotal.Text = "INR" + GetItemTotal();
}
protected void dlistProduct_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Addtocart")
{
string size = string.Empty;
foreach (DataListItem item in dlistProduct.Items)
{
RadioButtonList radio = (item.FindControl("rdlist") as RadioButtonList);
HiddenField hndID = (item.FindControl("hndID") as HiddenField);
if (Convert.ToInt16(e.CommandArgument) == Convert.ToInt16(hndID.Value))
{
size = radio.SelectedItem.Text;
break;
}
}
Shoppingcart scart = null;
List<Shoppingcart> cart = Session["Cart"] as List<Shoppingcart>;
var product = Session["Product"] as List<Product>;
var productdetails = product.Find(x => x.ID == Convert.ToInt16(e.CommandArgument));
if (cart != null)
{
var cardlist = cart.Find(x => x.ID == Convert.ToInt16(e.CommandArgument) && x.Size == size);
if (cardlist != null)
{
if (cardlist.ID == Convert.ToInt16(e.CommandArgument) && cardlist.Size == size)
{
cardlist.Quantity = cardlist.Quantity + 1;
cardlist.Cost = productdetails.Cost * cardlist.Quantity;
}
else if (cardlist.ID == Convert.ToInt16(e.CommandArgument))
{
cardlist.Quantity = cardlist.Quantity + 1;
cardlist.Cost = productdetails.Cost * cardlist.Quantity;
}
else
{
scart = AddNewProduct(size, scart, cart, productdetails);
}
}
else
{
scart = AddNewProduct(size, scart, cart, productdetails);
}
}
else
{
scart = new Shoppingcart();
scart.ID = productdetails.ID;
scart.Product = productdetails.ProductName;
scart.Quantity = 1;
scart.Size = size;
scart.Cost = productdetails.Cost;
cart = new List<Shoppingcart>();
cart.Add(scart);
}
Session["Cart"] = cart;
dg.DataSource = cart;
dg.DataBind();
lblTotal.Text = "$" + GetItemTotal();
}
}
protected void dlistProduct_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
HiddenField hndAU = (HiddenField)e.Item.FindControl("hndAU");
HiddenField hndBrandID = (HiddenField)e.Item.FindControl("hndBrandID");
Label lblBrandName = (Label)e.Item.FindControl("lblBrandName");
RadioButtonList rdlist = (RadioButtonList)e.Item.FindControl("rdlist");
rdlist.DataTextField = "Name";
rdlist.DataValueField = "ID";
rdlist.DataSource = cm.GetAvailableUnit(hndAU.Value);
rdlist.DataBind();
rdlist.SelectedIndex = 0;
lblBrandName.Text = cm.GetBrandName(Convert.ToInt16(hndBrandID.Value));
}
}
#endregion
#region Method
private void ShowProduct()
{
List<Product> productlist = new List<Product>() {
new Product{ID=1,CategoryID=2,BrandID=1,Availableunit="1,2",ProductName="Addidas T Shirt",ProductImage="Images/tshirt1.jpg",Cost=400 },
new Product{ID=2,CategoryID=2,BrandID=2,Availableunit="1,2,3",ProductName="Reebok T Shirt",ProductImage="Images/tshirt2.jpg",Cost=350 },
new Product{ID=3,CategoryID=3,BrandID=3,Availableunit="1,3",ProductName="Lee",ProductImage="Images/jeans.jpg",Cost=650 },
new Product{ID=4,CategoryID=3,BrandID=4,Availableunit="1,2,3",ProductName="Bare Denim",ProductImage="Images/jeans2.jpg",Cost=700 },
new Product{ID=5,CategoryID=1,BrandID=5,Availableunit="1,2",ProductName="Peter England",ProductImage="Images/shirt1.jpg",Cost=540 },
new Product{ID=6,CategoryID=1,BrandID=6,Availableunit="2,3",ProductName="Indian Terrain",ProductImage="Images/shirt2.jpg",Cost=566 }};
Session["Product"] = productlist;
dlistProduct.DataSource = productlist;
dlistProduct.DataBind();
}
private decimal GetItemTotal()
{
decimal decRunningTotal = default(decimal);
List<Shoppingcart> cart = Session["Cart"] as List<Shoppingcart>;
foreach (var lst in cart)
{
decRunningTotal += (Convert.ToDecimal(lst.Cost) * Convert.ToDecimal(lst.Quantity));
}
if (decRunningTotal > 0)
tblCart.Visible = true;
else
tblCart.Visible = false;
return decRunningTotal;
}
private static Shoppingcart AddNewProduct(string size, Shoppingcart scart, List<Shoppingcart> cart, Product productdetails)
{
scart = new Shoppingcart();
scart.ID = productdetails.ID;
scart.Product = productdetails.ProductName;
scart.Quantity = 1;
scart.Size = size;
scart.Cost = productdetails.Cost;
cart.Add(scart);
return scart;
}
#endregion
}
}
-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EcommerceApp.BLayer
{
public class Common
{
public List<AvailableUnit> GetAvailableUnit(string AuID)
{
List<AvailableUnit> list=new List<AvailableUnit>();
AvailableUnit au;
List<AvailableUnit> AUList = new List<AvailableUnit>() {
new AvailableUnit{ID=1,Name="Large"},
new AvailableUnit{ID=2,Name="Medium", },
new AvailableUnit{ID=3,Name="Small", }};
string[] strID = AuID.Split(',');
foreach (var str in strID)
{
var query = AUList.Find(s => s.ID ==Convert.ToInt16(str));
au = new AvailableUnit();
au.ID = query.ID;
au.Name = query.Name;
list.Add(au);
}
return list;
}
public string GetBrandName(int brandID)
{
string BrandName = string.Empty;
List<Brand> BrandList = new List<Brand>() {
new Brand{BrandID=1,BrandName="Addidas"},
new Brand{BrandID=2,BrandName="Reebok", },
new Brand{BrandID=3,BrandName="Lee"},
new Brand{BrandID=4,BrandName="Bare Denim", },
new Brand{BrandID=5,BrandName="Peter England",},
new Brand{BrandID=6,BrandName="Indian Terrain", }};
BrandName = BrandList.Find(x => x.BrandID == brandID).BrandName;
return BrandName;
}
}
}
-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
namespace EcommerceApp.BLayer
{
public class Category
{
#region All Public Methods are declare her
public int CategoryID { get; set; }
public int ParentID { get; set; }
public string CategoryName { get; set; }
DatabaseAcessLayer dbInfo = new DatabaseAcessLayer();
#endregion
#region All Public Methods are declare here
public Boolean InsertCategory()
{
SqlCommand cmd = new SqlCommand("USP_Category_Insert");
cmd.Parameters.AddWithValue("@ParentID", ParentID);
cmd.Parameters.AddWithValue("@CategoryName", CategoryName);
cmd.CommandType = CommandType.StoredProcedure;
if (dbInfo.InsertRecord(cmd))
return true;
else
return false;
}
public Boolean UpdateCategory()
{
SqlCommand cmd = new SqlCommand("USP_Category_Update");
cmd.Parameters.AddWithValue("@CategoryID", CategoryID);
cmd.Parameters.AddWithValue("@ParentID", ParentID);
cmd.Parameters.AddWithValue("@CategoryName", CategoryName);
cmd.CommandType = CommandType.StoredProcedure;
if (dbInfo.InsertRecord(cmd))
return true;
else
return false;
}
public Boolean DeleteCategory()
{
SqlCommand cmd = new SqlCommand("USP_Category_Delete");
cmd.Parameters.AddWithValue("@CategoryID", CategoryID);
cmd.CommandType = CommandType.StoredProcedure;
if (dbInfo.DeleteRecord(cmd))
return true;
else
return false;
}
public List<Category> GetCategoryDetailsbyID()
{
SqlCommand cmd = new SqlCommand("USP_Category_GetCategorybyID");
cmd.Parameters.AddWithValue("@CategoryID", CategoryID);
cmd.CommandType = CommandType.StoredProcedure;
List<Category> Categorylists = dbInfo.ExecuteReader<Category>(cmd);
return Categorylists;
}
public List<Category> GetAllCategoryDetails()
{
SqlCommand cmd = new SqlCommand("Usp_Category_GetAll");
List<Category> Categorylists = dbInfo.ExecuteReader<Category>(cmd);
return Categorylists;
}
#endregion
#region Constructor are declare here
public Category()
{
}
public Category(SqlDataReader reader)
{
CategoryID = (int)reader["CategoryID"];
ParentID = (int)reader["ParentID"];
CategoryName = (string)reader["CategoryName"];
}
#endregion
}
}
-----------------------------------------------------------------------------------------------------------
public class Product
{
#region All Public Methods are declare her
public int ID { get; set; }
public int CategoryID { get; set; }
public int BrandID { get; set; }
public string Availableunit { get; set; }
public string ProductImage { get; set; }
public string ProductName { get; set; }
public decimal Cost { get; set; }
DatabaseAcessLayer dbInfo = new DatabaseAcessLayer();
#endregion
#region Constructor are declare here
public Product()
{
}
public Product(SqlDataReader reader)
{
ID = (int)reader["ID"];
CategoryID = (int)reader["CategoryID"];
Availableunit = (string)reader["Availableunit"];
ProductImage = (string)reader["ProductImage"];
ProductName = (string)reader["ProductName"];
Cost = (Decimal)reader["Cost"];
}
#endregion
}
------------------------------------------------------------------------------------------------------------
public class AvailableUnit
{
public int ID { get; set; }
public string Name { get; set; }
}
public class Brand
{
public int BrandID { get; set; }
public string BrandName { get; set; }
}
public class Shoppingcart
{
public int ID { get; set; }
public int Quantity { get; set; }
public string Product { get; set; }
public string Size { get; set; }
public decimal Cost { get; set; }
}
------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Reflection;
namespace EcommerceApp.BLayer
{
public class DatabaseAcessLayer
{
#region All Private Member Variables are declare here
SqlConnection _SqlCon;
SqlDataAdapter _SqlDa;
private int _DBReturnInt;
private string _DBReturnString;
private decimal _DBReturnDecimal;
# endregion
#region All Property of Private Member Variables are declare here
public decimal DBReturnDecimal
{
get
{
return _DBReturnDecimal;
}
}
public int DBReturnInt
{
get
{
return _DBReturnInt;
}
}
public string DBReturnString
{
get
{
return _DBReturnString;
}
}
# endregion
#region Constructor
public DatabaseAcessLayer()
{
_SqlCon = new SqlConnection(Convert.ToString(ConfigurationManager.ConnectionStrings["Constr"]));
}
#endregion
#region Method
public Boolean InsertRecord(SqlCommand _SqlCmd)
{
Int32 _RowAffect = ExecuteNonQuery(_SqlCmd);
if (_RowAffect == 1)
return true;
else
return false;
}
public Boolean UpdateRecord(SqlCommand _SqlCmd)
{
Int32 _RowAffect = ExecuteNonQuery(_SqlCmd);
if (_RowAffect >= 1)
return true;
else
return false;
}
public Boolean DeleteRecord(SqlCommand _SqlCmd)
{
Int32 _RowAffect = ExecuteNonQuery(_SqlCmd);
if (_RowAffect >= 1)
return true;
else
return false;
}
public List<T> ExecuteReader<T>(SqlCommand _SqlCmd)
{
SqlDataReader dr;
OpenConnection();
_SqlCmd.Connection = _SqlCon;
dr = _SqlCmd.ExecuteReader(CommandBehavior.CloseConnection);
List<T> list = new List<T>();
T obj = default(T);
using (_SqlCon)
{
while (dr.Read())
{
obj = Activator.CreateInstance<T>();
foreach (PropertyInfo prop in obj.GetType().GetProperties())
{
try
{
if (!object.Equals(dr[prop.Name], DBNull.Value))
{
prop.SetValue(obj, dr[prop.Name], null);
}
}
catch (IndexOutOfRangeException ex)
{
}
}
list.Add(obj);
}
}
return list;
}
private int ExecuteNonQuery(SqlCommand _SqlCmd)
{
Int32 _RowAffect = 0;
try
{
if (_SqlCmd != null)
{
OpenConnection();
_SqlCmd.Connection = _SqlCon;
_RowAffect = _SqlCmd.ExecuteNonQuery();
}
}
catch { throw; }
finally
{
CloseConnection();
}
return _RowAffect;
}
private void CloseConnection()
{
if (_SqlCon.State == ConnectionState.Open)
{
_SqlCon.Close();
}
}
private void OpenConnection()
{
if (_SqlCon.State == ConnectionState.Closed)
{
_SqlCon.Open();
}
}
#endregion
}
}
<html>
<head>
<title>Shopping Cart</title>
<script runat="server">
Dim objDT As System.Data.DataTable
Dim objDR As System.Data.DataRow
Private Sub Page_Load(s As Object, e As EventArgs)
If Not IsPostBack Then
makeCart()
End If
End Sub
Function makeCart()
objDT = New System.Data.DataTable("Cart")
objDT.Columns.Add("ID", GetType(Integer))
objDT.Columns("ID").AutoIncrement = True
objDT.Columns("ID").AutoIncrementSeed = 1
objDT.Columns.Add("Quantity", GetType(Integer))
objDT.Columns.Add("Product", GetType(String))
objDT.Columns.Add("Cost", GetType(Decimal))
Session("Cart") = objDT
End Function
Sub AddToCart(s As Object, e As EventArgs)
objDT = Session("Cart")
Dim Product = ddlProducts.SelectedItem.Text
Dim blnMatch As Boolean = False
For Each objDR In objDT.Rows
If objDR("Product") = Product Then
objDR("Quantity") += txtQuantity.Text
blnMatch = True
Exit For
End If
Next
If Not blnMatch Then
objDR = objDT.NewRow
objDR("Quantity") = txtQuantity.Text
objDR("Product") = ddlProducts.SelectedItem.Text
objDR("Cost") = Decimal.Parse(ddlProducts.SelectedItem.Value)
objDT.Rows.Add(objDR)
End If
Session("Cart") = objDT
dg.DataSource = objDT
dg.DataBind()
lblTotal.Text = "$" & GetItemTotal()
End Sub
Function GetItemTotal() As Decimal
Dim intCounter As Integer
Dim decRunningTotal As Decimal
For intCounter = 0 To objDT.Rows.Count - 1
objDR = objDT.Rows(intCounter)
decRunningTotal += (objDR("Cost") * objDR("Quantity"))
Next
Return decRunningTotal
End Function
Sub Delete_Item(s As Object, e As DataGridCommandEventArgs)
objDT = Session("Cart")
objDT.Rows(e.Item.ItemIndex).Delete()
Session("Cart") = objDT
dg.DataSource = objDT
dg.DataBind()
lblTotal.Text = "$" & GetItemTotal()
End Sub
</script>
</head>
<body>
<form runat="server">
Product:<br>
<asp:DropDownList id=ddlProducts runat="server">
<asp:ListItem Value="4.99">Socks</asp:ListItem>
<asp:ListItem Value="34.99">Pants</asp:ListItem>
<asp:ListItem Value="14.99">Shirt</asp:ListItem>
<asp:ListItem Value="12.99">Hat</asp:ListItem>
</asp:DropDownList><br>
Quantity:<br>
<asp:textbox id="txtQuantity" runat="server" /><br><br>
<asp:Button id=btnAdd runat="server" Text="Add To Cart" onClick="AddToCart" /><br><br>
<asp:DataGrid id=dg runat="server" ondeletecommand="Delete_Item">
<columns>
<asp:buttoncolumn buttontype="LinkButton" commandname="Delete" text="Remove Item" />
</columns>
</asp:DataGrid>
<br><br>
Total:
<asp:Label id=lblTotal runat="server" />
</form>
</body>
</html>
-----------------------------------------------------------------------------------------------------------
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="EcommerceApp._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager ID="ScriptManager1" ScriptMode="Release" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table width="80%">
<tr>
<td align="left" valign="top" style="width: 70%">
<asp:DataList Width="100%" ID="dlistProduct" runat="server" RepeatColumns="2" DataKeyField="ID"
OnItemCommand="dlistProduct_ItemCommand" OnItemDataBound="dlistProduct_ItemDataBound">
<ItemTemplate>
<table>
<tr>
<td>
<img id="imgProduct" src='<%#Eval("ProductImage")%>' height="100px" width="100px" />
<asp:HiddenField ID="hndID" runat="server" Value='<%#Eval("ID")%>' />
</td>
<td>
<table>
<tr>
<td>
<b>Name:</b>
<%#Eval("ProductName")%>
</td>
</tr>
<tr>
<td>
<b>Brand:</b>
<asp:HiddenField ID="hndBrandID" runat="server" Value='<%#Eval("BrandID")%>' />
<asp:Label ID="lblBrandName" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<b>Cost:</b>
<%#Eval("Cost")%>
INR
</td>
</tr>
<tr>
<td>
<asp:HiddenField ID="hndAU" runat="server" Value='<%#Eval("Availableunit")%>' />
<table>
<tr>
<td>
<b>Size:</b>
</td>
<td>
<asp:RadioButtonList ID="rdlist" RepeatDirection="Vertical" runat="server">
</asp:RadioButtonList>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnAddtocart" runat="server" CommandArgument='<%#Eval("ID")%>' CommandName="Addtocart"
Text="Add To Cart" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</td>
<td align="right" valign="top" style="width: 30%">
<table id="tblCart" runat="server" visible="false" border="1" style="border-color: #336666;
border-bottom-style: solid">
<tr>
<td align="left" valign="top">
<asp:DataGrid ID="dg" DataKeyField="ID" runat="server" AutoGenerateColumns="false"
OnDeleteCommand="Delete_Item" BackColor="White" CellPadding="2" GridLines="Horizontal">
<Columns>
<asp:BoundColumn DataField="Product" HeaderText="Product"></asp:BoundColumn>
<asp:BoundColumn DataField="Size" HeaderText="Size"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Quantity">
<ItemTemplate>
<asp:Label ID="lblQty" runat="server" Text='<%#Eval("Quantity")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtQty" runat="server" Text='<%#Eval("Quantity")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Cost" HeaderText="Cost"></asp:BoundColumn>
<asp:ButtonColumn ButtonType="LinkButton" CommandName="Delete" Text="Remove" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<ItemStyle ForeColor="#333333" BackColor="White" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" Mode="NumericPages" />
<SelectedItemStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>
</td>
</tr>
<tr>
<td align="left" valign="top">
Total:
<asp:Label ID="lblTotal" runat="server" />
</td>
</tr>
<tr>
<td align="center" valign="top">
<asp:Button ID="btnCheckout" runat="server" Text="CheckOut" PostBackUrl="OrderDetail.aspx" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
-----------------------------------------------------------------------------------------------------
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 EcommerceApp.BLayer;
namespace EcommerceApp
{
public partial class _Default : System.Web.UI.Page
{
#region Event
Common cm = new Common();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowProduct();
List<Shoppingcart> cart = Session["Cart"] as List<Shoppingcart>;
if (cart != null && cart.Count() > 0)
{
dg.DataSource = cart;
dg.DataBind();
lblTotal.Text = "$" + GetItemTotal();
}
}
}
public void Delete_Item(object s, DataGridCommandEventArgs e)
{
List<Shoppingcart> cart = Session["Cart"] as List<Shoppingcart>;
cart.Remove(cart[e.Item.ItemIndex]);
Session["Cart"] = cart;
dg.DataSource = cart;
dg.DataBind();
lblTotal.Text = "INR" + GetItemTotal();
}
protected void dlistProduct_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Addtocart")
{
string size = string.Empty;
foreach (DataListItem item in dlistProduct.Items)
{
RadioButtonList radio = (item.FindControl("rdlist") as RadioButtonList);
HiddenField hndID = (item.FindControl("hndID") as HiddenField);
if (Convert.ToInt16(e.CommandArgument) == Convert.ToInt16(hndID.Value))
{
size = radio.SelectedItem.Text;
break;
}
}
Shoppingcart scart = null;
List<Shoppingcart> cart = Session["Cart"] as List<Shoppingcart>;
var product = Session["Product"] as List<Product>;
var productdetails = product.Find(x => x.ID == Convert.ToInt16(e.CommandArgument));
if (cart != null)
{
var cardlist = cart.Find(x => x.ID == Convert.ToInt16(e.CommandArgument) && x.Size == size);
if (cardlist != null)
{
if (cardlist.ID == Convert.ToInt16(e.CommandArgument) && cardlist.Size == size)
{
cardlist.Quantity = cardlist.Quantity + 1;
cardlist.Cost = productdetails.Cost * cardlist.Quantity;
}
else if (cardlist.ID == Convert.ToInt16(e.CommandArgument))
{
cardlist.Quantity = cardlist.Quantity + 1;
cardlist.Cost = productdetails.Cost * cardlist.Quantity;
}
else
{
scart = AddNewProduct(size, scart, cart, productdetails);
}
}
else
{
scart = AddNewProduct(size, scart, cart, productdetails);
}
}
else
{
scart = new Shoppingcart();
scart.ID = productdetails.ID;
scart.Product = productdetails.ProductName;
scart.Quantity = 1;
scart.Size = size;
scart.Cost = productdetails.Cost;
cart = new List<Shoppingcart>();
cart.Add(scart);
}
Session["Cart"] = cart;
dg.DataSource = cart;
dg.DataBind();
lblTotal.Text = "$" + GetItemTotal();
}
}
protected void dlistProduct_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
HiddenField hndAU = (HiddenField)e.Item.FindControl("hndAU");
HiddenField hndBrandID = (HiddenField)e.Item.FindControl("hndBrandID");
Label lblBrandName = (Label)e.Item.FindControl("lblBrandName");
RadioButtonList rdlist = (RadioButtonList)e.Item.FindControl("rdlist");
rdlist.DataTextField = "Name";
rdlist.DataValueField = "ID";
rdlist.DataSource = cm.GetAvailableUnit(hndAU.Value);
rdlist.DataBind();
rdlist.SelectedIndex = 0;
lblBrandName.Text = cm.GetBrandName(Convert.ToInt16(hndBrandID.Value));
}
}
#endregion
#region Method
private void ShowProduct()
{
List<Product> productlist = new List<Product>() {
new Product{ID=1,CategoryID=2,BrandID=1,Availableunit="1,2",ProductName="Addidas T Shirt",ProductImage="Images/tshirt1.jpg",Cost=400 },
new Product{ID=2,CategoryID=2,BrandID=2,Availableunit="1,2,3",ProductName="Reebok T Shirt",ProductImage="Images/tshirt2.jpg",Cost=350 },
new Product{ID=3,CategoryID=3,BrandID=3,Availableunit="1,3",ProductName="Lee",ProductImage="Images/jeans.jpg",Cost=650 },
new Product{ID=4,CategoryID=3,BrandID=4,Availableunit="1,2,3",ProductName="Bare Denim",ProductImage="Images/jeans2.jpg",Cost=700 },
new Product{ID=5,CategoryID=1,BrandID=5,Availableunit="1,2",ProductName="Peter England",ProductImage="Images/shirt1.jpg",Cost=540 },
new Product{ID=6,CategoryID=1,BrandID=6,Availableunit="2,3",ProductName="Indian Terrain",ProductImage="Images/shirt2.jpg",Cost=566 }};
Session["Product"] = productlist;
dlistProduct.DataSource = productlist;
dlistProduct.DataBind();
}
private decimal GetItemTotal()
{
decimal decRunningTotal = default(decimal);
List<Shoppingcart> cart = Session["Cart"] as List<Shoppingcart>;
foreach (var lst in cart)
{
decRunningTotal += (Convert.ToDecimal(lst.Cost) * Convert.ToDecimal(lst.Quantity));
}
if (decRunningTotal > 0)
tblCart.Visible = true;
else
tblCart.Visible = false;
return decRunningTotal;
}
private static Shoppingcart AddNewProduct(string size, Shoppingcart scart, List<Shoppingcart> cart, Product productdetails)
{
scart = new Shoppingcart();
scart.ID = productdetails.ID;
scart.Product = productdetails.ProductName;
scart.Quantity = 1;
scart.Size = size;
scart.Cost = productdetails.Cost;
cart.Add(scart);
return scart;
}
#endregion
}
}
-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EcommerceApp.BLayer
{
public class Common
{
public List<AvailableUnit> GetAvailableUnit(string AuID)
{
List<AvailableUnit> list=new List<AvailableUnit>();
AvailableUnit au;
List<AvailableUnit> AUList = new List<AvailableUnit>() {
new AvailableUnit{ID=1,Name="Large"},
new AvailableUnit{ID=2,Name="Medium", },
new AvailableUnit{ID=3,Name="Small", }};
string[] strID = AuID.Split(',');
foreach (var str in strID)
{
var query = AUList.Find(s => s.ID ==Convert.ToInt16(str));
au = new AvailableUnit();
au.ID = query.ID;
au.Name = query.Name;
list.Add(au);
}
return list;
}
public string GetBrandName(int brandID)
{
string BrandName = string.Empty;
List<Brand> BrandList = new List<Brand>() {
new Brand{BrandID=1,BrandName="Addidas"},
new Brand{BrandID=2,BrandName="Reebok", },
new Brand{BrandID=3,BrandName="Lee"},
new Brand{BrandID=4,BrandName="Bare Denim", },
new Brand{BrandID=5,BrandName="Peter England",},
new Brand{BrandID=6,BrandName="Indian Terrain", }};
BrandName = BrandList.Find(x => x.BrandID == brandID).BrandName;
return BrandName;
}
}
}
-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
namespace EcommerceApp.BLayer
{
public class Category
{
#region All Public Methods are declare her
public int CategoryID { get; set; }
public int ParentID { get; set; }
public string CategoryName { get; set; }
DatabaseAcessLayer dbInfo = new DatabaseAcessLayer();
#endregion
#region All Public Methods are declare here
public Boolean InsertCategory()
{
SqlCommand cmd = new SqlCommand("USP_Category_Insert");
cmd.Parameters.AddWithValue("@ParentID", ParentID);
cmd.Parameters.AddWithValue("@CategoryName", CategoryName);
cmd.CommandType = CommandType.StoredProcedure;
if (dbInfo.InsertRecord(cmd))
return true;
else
return false;
}
public Boolean UpdateCategory()
{
SqlCommand cmd = new SqlCommand("USP_Category_Update");
cmd.Parameters.AddWithValue("@CategoryID", CategoryID);
cmd.Parameters.AddWithValue("@ParentID", ParentID);
cmd.Parameters.AddWithValue("@CategoryName", CategoryName);
cmd.CommandType = CommandType.StoredProcedure;
if (dbInfo.InsertRecord(cmd))
return true;
else
return false;
}
public Boolean DeleteCategory()
{
SqlCommand cmd = new SqlCommand("USP_Category_Delete");
cmd.Parameters.AddWithValue("@CategoryID", CategoryID);
cmd.CommandType = CommandType.StoredProcedure;
if (dbInfo.DeleteRecord(cmd))
return true;
else
return false;
}
public List<Category> GetCategoryDetailsbyID()
{
SqlCommand cmd = new SqlCommand("USP_Category_GetCategorybyID");
cmd.Parameters.AddWithValue("@CategoryID", CategoryID);
cmd.CommandType = CommandType.StoredProcedure;
List<Category> Categorylists = dbInfo.ExecuteReader<Category>(cmd);
return Categorylists;
}
public List<Category> GetAllCategoryDetails()
{
SqlCommand cmd = new SqlCommand("Usp_Category_GetAll");
List<Category> Categorylists = dbInfo.ExecuteReader<Category>(cmd);
return Categorylists;
}
#endregion
#region Constructor are declare here
public Category()
{
}
public Category(SqlDataReader reader)
{
CategoryID = (int)reader["CategoryID"];
ParentID = (int)reader["ParentID"];
CategoryName = (string)reader["CategoryName"];
}
#endregion
}
}
-----------------------------------------------------------------------------------------------------------
public class Product
{
#region All Public Methods are declare her
public int ID { get; set; }
public int CategoryID { get; set; }
public int BrandID { get; set; }
public string Availableunit { get; set; }
public string ProductImage { get; set; }
public string ProductName { get; set; }
public decimal Cost { get; set; }
DatabaseAcessLayer dbInfo = new DatabaseAcessLayer();
#endregion
#region Constructor are declare here
public Product()
{
}
public Product(SqlDataReader reader)
{
ID = (int)reader["ID"];
CategoryID = (int)reader["CategoryID"];
Availableunit = (string)reader["Availableunit"];
ProductImage = (string)reader["ProductImage"];
ProductName = (string)reader["ProductName"];
Cost = (Decimal)reader["Cost"];
}
#endregion
}
------------------------------------------------------------------------------------------------------------
public class AvailableUnit
{
public int ID { get; set; }
public string Name { get; set; }
}
public class Brand
{
public int BrandID { get; set; }
public string BrandName { get; set; }
}
public class Shoppingcart
{
public int ID { get; set; }
public int Quantity { get; set; }
public string Product { get; set; }
public string Size { get; set; }
public decimal Cost { get; set; }
}
------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Reflection;
namespace EcommerceApp.BLayer
{
public class DatabaseAcessLayer
{
#region All Private Member Variables are declare here
SqlConnection _SqlCon;
SqlDataAdapter _SqlDa;
private int _DBReturnInt;
private string _DBReturnString;
private decimal _DBReturnDecimal;
# endregion
#region All Property of Private Member Variables are declare here
public decimal DBReturnDecimal
{
get
{
return _DBReturnDecimal;
}
}
public int DBReturnInt
{
get
{
return _DBReturnInt;
}
}
public string DBReturnString
{
get
{
return _DBReturnString;
}
}
# endregion
#region Constructor
public DatabaseAcessLayer()
{
_SqlCon = new SqlConnection(Convert.ToString(ConfigurationManager.ConnectionStrings["Constr"]));
}
#endregion
#region Method
public Boolean InsertRecord(SqlCommand _SqlCmd)
{
Int32 _RowAffect = ExecuteNonQuery(_SqlCmd);
if (_RowAffect == 1)
return true;
else
return false;
}
public Boolean UpdateRecord(SqlCommand _SqlCmd)
{
Int32 _RowAffect = ExecuteNonQuery(_SqlCmd);
if (_RowAffect >= 1)
return true;
else
return false;
}
public Boolean DeleteRecord(SqlCommand _SqlCmd)
{
Int32 _RowAffect = ExecuteNonQuery(_SqlCmd);
if (_RowAffect >= 1)
return true;
else
return false;
}
public List<T> ExecuteReader<T>(SqlCommand _SqlCmd)
{
SqlDataReader dr;
OpenConnection();
_SqlCmd.Connection = _SqlCon;
dr = _SqlCmd.ExecuteReader(CommandBehavior.CloseConnection);
List<T> list = new List<T>();
T obj = default(T);
using (_SqlCon)
{
while (dr.Read())
{
obj = Activator.CreateInstance<T>();
foreach (PropertyInfo prop in obj.GetType().GetProperties())
{
try
{
if (!object.Equals(dr[prop.Name], DBNull.Value))
{
prop.SetValue(obj, dr[prop.Name], null);
}
}
catch (IndexOutOfRangeException ex)
{
}
}
list.Add(obj);
}
}
return list;
}
private int ExecuteNonQuery(SqlCommand _SqlCmd)
{
Int32 _RowAffect = 0;
try
{
if (_SqlCmd != null)
{
OpenConnection();
_SqlCmd.Connection = _SqlCon;
_RowAffect = _SqlCmd.ExecuteNonQuery();
}
}
catch { throw; }
finally
{
CloseConnection();
}
return _RowAffect;
}
private void CloseConnection()
{
if (_SqlCon.State == ConnectionState.Open)
{
_SqlCon.Close();
}
}
private void OpenConnection()
{
if (_SqlCon.State == ConnectionState.Closed)
{
_SqlCon.Open();
}
}
#endregion
}
}
No comments:
Post a Comment