Shopping Cart in asp.net
create database script
CREATE TABLE [dbo].[tbl_Product](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[Image] [nvarchar](200) NULL,
[Title] [nvarchar](50) NULL,
[description] [nvarchar](max) NULL,
[our_price] [bigint] NULL,
[size] [nvarchar](20) NULL,
[Product_quantity] [bigint] NULL CONSTRAINT [DF_tbl_Product_Product_quantity] DEFAULT ((0)),
[Product_code] [nvarchar](20) NULL,
[date] [datetime] NULL CONSTRAINT [DF_tbl_Product_date] DEFAULT (getdate()),
CONSTRAINT [PK_tbl_Product] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
now
add some dummy data like this
INSERT INTO [tbl_Product] VALUES('pics/Water lilies.jpg','water','water me',23,8,9,'abo3',getdate())
create two pages named
1. products.aspx
2.shoppingcart.aspx
html coding of product pages
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Products.aspx.cs" Inherits="Products" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" DataKeyField="id"
CellPadding="15" Width="508px">
<ItemTemplate>
<a href='Shopping_Cart.aspx?cart_id=<%# Eval("id") %>'>
<asp:Image ID="img" runat="server" ImageUrl='<%#Eval("Image") %>' Width="50px" />
</a>
<h4><asp:Label ID="lb" runat="server" Font-Bold="True" Font-Size="12pt" Text='<%# Eval("Title") %>'></asp:Label><br />
Product Code: <span><%# Eval("product_code") %></span></h4>
<span ><del>र</del><%# Eval("Our_price") %> INR</span><br />
<a href='Shopping_Cart.aspx?cart_id=<%# Eval("id") %>'>Add to Cart</a>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
product.aspx codebehind
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Products : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
SqlDataAdapter adp = new SqlDataAdapter();
SqlCommand cmd;
DataTable tb;
DataTable dt = new DataTable();
//string a, b;
//string a, save, fn, b, aguid,fn1,aguid1,save1;
private PagedDataSource pagedData = new PagedDataSource();
decimal last1;
Int32 count, no;
protected void Page_Load(object sender, EventArgs e)
{
con=new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
try
{
con.Open();
//TeSection_txt.Text = Request.QueryString["a"].ToString();
if (Page.IsPostBack == false)
{
try
{
doPaging();
}
catch
{
}
}
}
catch
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
}
con.Close();
}
//fetching the records
public DataTable getTheData()
{
dt = new DataTable();
adp = new SqlDataAdapter("select * from tbl_Product", con);
adp.Fill(dt);
adp.Dispose();
DataList1.DataSource = dt;
DataList1.DataBind();
if (dt.Rows.Count == 0)
{
//lbl_msg.Text = "Gallery is empty ";
}
else
{
Session["cnt"] = Convert.ToInt32((dt.Rows.Count));
}
return dt;
}
private void doPaging()
{
//calling the getTheData() and fillin into pagedDataSource.
pagedData.DataSource = getTheData().DefaultView;
pagedData.AllowPaging = true;
pagedData.PageSize = 3;
count = Convert.ToInt32(Session["cnt"]);
last1 = count / pagedData.PageSize; //total record
if (count % pagedData.PageSize == 0)
{
last1 = -last1;
}
last1 = Math.Ceiling(last1);
// Last record this is becaz if u have 3 record and displayin 2 in one page the 3rd on will display on another page….
try
{
pagedData.CurrentPageIndex = Int32.Parse(Request["Page"].ToString());
}
catch
{
pagedData.CurrentPageIndex = 0;
}
//for paging u can use following code.
//Prv_btn.Visible = (!pagedData.IsFirstPage);
//first_btn.Visible = (!pagedData.IsFirstPage);
//Next_btn.Visible = (!pagedData.IsLastPage);
//Last_btn.Visible = (!pagedData.IsLastPage);
DataList1.DataSource = pagedData;
DataList1.DataBind();
}
}
design looks like -
create database script
CREATE TABLE [dbo].[tbl_Product](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[Image] [nvarchar](200) NULL,
[Title] [nvarchar](50) NULL,
[description] [nvarchar](max) NULL,
[our_price] [bigint] NULL,
[size] [nvarchar](20) NULL,
[Product_quantity] [bigint] NULL CONSTRAINT [DF_tbl_Product_Product_quantity] DEFAULT ((0)),
[Product_code] [nvarchar](20) NULL,
[date] [datetime] NULL CONSTRAINT [DF_tbl_Product_date] DEFAULT (getdate()),
CONSTRAINT [PK_tbl_Product] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
now
add some dummy data like this
INSERT INTO [tbl_Product] VALUES('pics/Water lilies.jpg','water','water me',23,8,9,'abo3',getdate())
create two pages named
1. products.aspx
2.shoppingcart.aspx
html coding of product pages
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Products.aspx.cs" Inherits="Products" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" DataKeyField="id"
CellPadding="15" Width="508px">
<ItemTemplate>
<a href='Shopping_Cart.aspx?cart_id=<%# Eval("id") %>'>
<asp:Image ID="img" runat="server" ImageUrl='<%#Eval("Image") %>' Width="50px" />
</a>
<h4><asp:Label ID="lb" runat="server" Font-Bold="True" Font-Size="12pt" Text='<%# Eval("Title") %>'></asp:Label><br />
Product Code: <span><%# Eval("product_code") %></span></h4>
<span ><del>र</del><%# Eval("Our_price") %> INR</span><br />
<a href='Shopping_Cart.aspx?cart_id=<%# Eval("id") %>'>Add to Cart</a>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
product.aspx codebehind
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Products : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
SqlDataAdapter adp = new SqlDataAdapter();
SqlCommand cmd;
DataTable tb;
DataTable dt = new DataTable();
//string a, b;
//string a, save, fn, b, aguid,fn1,aguid1,save1;
private PagedDataSource pagedData = new PagedDataSource();
decimal last1;
Int32 count, no;
protected void Page_Load(object sender, EventArgs e)
{
con=new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
try
{
con.Open();
//TeSection_txt.Text = Request.QueryString["a"].ToString();
if (Page.IsPostBack == false)
{
try
{
doPaging();
}
catch
{
}
}
}
catch
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
}
con.Close();
}
//fetching the records
public DataTable getTheData()
{
dt = new DataTable();
adp = new SqlDataAdapter("select * from tbl_Product", con);
adp.Fill(dt);
adp.Dispose();
DataList1.DataSource = dt;
DataList1.DataBind();
if (dt.Rows.Count == 0)
{
//lbl_msg.Text = "Gallery is empty ";
}
else
{
Session["cnt"] = Convert.ToInt32((dt.Rows.Count));
}
return dt;
}
private void doPaging()
{
//calling the getTheData() and fillin into pagedDataSource.
pagedData.DataSource = getTheData().DefaultView;
pagedData.AllowPaging = true;
pagedData.PageSize = 3;
count = Convert.ToInt32(Session["cnt"]);
last1 = count / pagedData.PageSize; //total record
if (count % pagedData.PageSize == 0)
{
last1 = -last1;
}
last1 = Math.Ceiling(last1);
// Last record this is becaz if u have 3 record and displayin 2 in one page the 3rd on will display on another page….
try
{
pagedData.CurrentPageIndex = Int32.Parse(Request["Page"].ToString());
}
catch
{
pagedData.CurrentPageIndex = 0;
}
//for paging u can use following code.
//Prv_btn.Visible = (!pagedData.IsFirstPage);
//first_btn.Visible = (!pagedData.IsFirstPage);
//Next_btn.Visible = (!pagedData.IsLastPage);
//Last_btn.Visible = (!pagedData.IsLastPage);
DataList1.DataSource = pagedData;
DataList1.DataBind();
}
}
design looks like -
No comments:
Post a Comment