Guys, I have already created a cart and data is also coming in cart whenever i click on add to cart button but when i refreshes cart page existing item is again added in cart and again and again whenever i refreshesh cart page, Please can anyone tell me what i m doing wrong here..............PLEASE
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.Sql;
using System.Data.SqlClient;
public partial class addtocart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("sno");
dt.Columns.Add("id");
dt.Columns.Add("name");
dt.Columns.Add("description");
dt.Columns.Add("image");
dt.Columns.Add("price");
if (Request.QueryString["id"] != null)
{
if (Session["Buyitems"] == null)
{
dr = dt.NewRow();
String mycon = "Data Source=DESKTOP-5PT7B1R;Initial Catalog=Main_Database;Integrated Security=True";
SqlConnection con = new SqlConnection(mycon);
String myquery = "select * from Product_Update where id=" + Request.QueryString["id"];
SqlCommand cmd = new SqlCommand();
cmd.CommandText = myquery;
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
dr["sno"] = 1;
dr["id"] = ds.Tables[0].Rows[0]["id"].ToString();
dr["name"] = ds.Tables[0].Rows[0]["name"].ToString();
dr["description"] = ds.Tables[0].Rows[0]["description"].ToString();
dr["image"] = ds.Tables[0].Rows[0]["image"].ToString();
dr["price"] = ds.Tables[0].Rows[0]["price"].ToString();
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
Session["buyitems"] = dt;
}
else
{
dt = (DataTable)Session["buyitems"];
int sr;
sr = dt.Rows.Count;
dr = dt.NewRow();
String mycon = "Data Source=DESKTOP-5PT7B1R;Initial Catalog=Main_Database;Integrated Security=True";
SqlConnection scon = new SqlConnection(mycon);
String myquery = "select * from Product_Update where id=" + Request.QueryString["id"];
SqlCommand cmd = new SqlCommand();
cmd.CommandText = myquery;
cmd.Connection = scon;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
dr["sno"] = sr + 1;
dr["id"] = ds.Tables[0].Rows[0]["id"].ToString();
dr["name"] = ds.Tables[0].Rows[0]["name"].ToString();
dr["description"] = ds.Tables[0].Rows[0]["description"].ToString();
dr["image"] = ds.Tables[0].Rows[0]["image"].ToString();
dr["price"] = ds.Tables[0].Rows[0]["price"].ToString();
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
Session["buyitems"] = dt;
}
}
else
{
dt = (DataTable)Session["buyitems"];
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="addtocart.aspx.cs" Inherits="addtocart" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Welcome To Shoppify.com </h1>
</div>
<div>
<asp:Label ID="Label1" runat="server" Text="This Page shows product added by Customers"></asp:Label>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="~/DataList_Control_retriev.aspx">HyperLink</asp:HyperLink>
</div>
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Height="421px" Width="1033px" BackColor="#DEBA84" BorderColor="#DEBA84"
BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<Columns>
<asp:BoundField DataField="id" HeaderText="SNO" />
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="name" HeaderText="NAME" />
<asp:BoundField DataField="description" HeaderText="Description" />
<asp:ImageField DataImageUrlField="image" HeaderText="IMAGE">
<ControlStyle Height="100px" Width="100px" />
<ItemStyle HorizontalAlign="Center" />
</asp:ImageField>
<asp:BoundField DataField="price" HeaderText="PRICE" />
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>