将产品添加到会话中。 [英] Adding the products into the session.

查看:94
本文介绍了将产品添加到会话中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家好,我正在使用c#asp.net中的图层架构进行小型购物网站项目,现在我正在使用cart_page在cart_page中添加产品。下面是代码&我用来添加产品但效果不好,产品无法在会话中添加。所以请帮帮我





Hello everybody, I am making on a small shopping website project using layer architecture in c# asp.net and now I am working on the cart_page to add the products in the cart_page.Here's below is the code & I use to add the products but it can't works well and products can't be add in the session.So please help me


public partial class Default2 : System.Web.UI.Page
{
    DataTable Basket_DataTable = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["basket"] != null)
        {
            Basket_DataTable = (DataTable)Session["basket"];
        }
        else
        {
            Basket_DataTable = new DataTable();
            Basket_DataTable.Columns.Add("Id");
            Basket_DataTable.Columns.Add("Name");
            Basket_DataTable.Columns.Add("ImageUrl");
            Basket_DataTable.Columns.Add("Price");
            Basket_DataTable.Columns.Add("Item");
            //Basket_DataTable.Columns.Add("total");
        }

        if (Request["DelId"] != null)
        {
            for (int i = 0; i < Basket_DataTable.Rows.Count; i++)
            {
                if (Basket_DataTable.Rows[i][0].ToString() == Request["DelId"].ToString())
                    Basket_DataTable.Rows.Remove(Basket_DataTable.Rows[i]);
            }
        }
        if (Request["Id"] != null)
        {
            bool Found = false;
            for (int i = 0; i < Basket_DataTable.Rows.Count; i++)
            {
                if (Basket_DataTable.Rows[i][0].ToString() == Request["Id"].ToString())
                    Found = true;

            }
            if (Found == false)
            {
                DataAccess.TestDA data = new DataAccess.TestDA();
                string sql = "Select * from Products where Id=" + Request["Id"];
                DataTable ret = data.exe_select(sql);
                if (ret != null && ret.Rows.Count == 1)
                {
                    Basket_DataTable.Rows.Add(new object[]{Request["Id"],ret.Rows[0]["Name"].ToString(),
                      ret.Rows[0]["Price"].ToString(),ret.Rows[0]["ImageUrl"].ToString()
                      ,"1",ret.Rows[0]["Price"].ToString()});


                }

            }
        }
        if (IsPostBack == false)
        {`enter code here`enter code here`
            //GridView2.DataSource = Basket_DataTable;
            GridView2.DataBind();
        }

        Session["basket"] = Basket_DataTable;
    }

推荐答案

public partial class Default2 : System.Web.UI.Page
{
    DataTable Basket_DataTable = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["basket"] != null)
        {
            Basket_DataTable = (DataTable)Session["basket"];
        }
        else
        {
            Basket_DataTable = new DataTable();
            Basket_DataTable.Columns.Add("Id", Type.GetType("System.Int32")));
            Basket_DataTable.Columns.Add("Name", Type.GetType("System.String")));
            Basket_DataTable.Columns.Add("ImageUrl", Type.GetType("System.String")));
            Basket_DataTable.Columns.Add("Price", Type.GetType("System.Int32")));
            Basket_DataTable.Columns.Add("Item", Type.GetType("System.Int32"))););
            //Basket_DataTable.Columns.Add("total", Type.GetType("System.Int32"))););
        }
 
int id = 0;
        if (Request["DelId"] != null)
        {
            id = (int)Request["DelId"];
            DataRow() deleteRows = Basket_DataTable.Select(String.Format("Id={0}", id));
            if (deleteRows.Lenght = 1)
                    Basket_DataTable.Rows.Remove(deleteRows[0]);
        }
id = 0;
        if (Request["Id"] != null)
        {
id = (int)Request["Id"];
DataRow() findRow = Basket_DataTable.Select(String.Format("Id={0}", id));
 
            if (findRow.Length = 0) // not found
            {
                DataAccess.TestDA data = new DataAccess.TestDA();
// HERE YOU HAVE A THREAT OF SQL INJECTION - YOU SHOULD NEVER CONCATENATE SOMETHING THAT USERS CAN CHANGE!!! At the very least use HtmlServer.Encode
//Better would be to use stored procedure or parametrized query

                string sql = "Select id, name, price, imageurl, item = 1, totak = price from Products where Id=" + id.ToString;

                DataTable ret = data.exe_select(sql);
                if (ret != null && ret.Rows.Count == 1)
                {
// This will work if your basket and the row retrieved have the same columns,
// if they don't, change the select * to inlcude only relevant columns (as I did)
Basket_DataTable.ImportRow(ret);

                }
 
            }
else // found, increase the quantity of the item by one
findRow[0]["Item"] = (int)findRow[0]["Item"] +1;
        }

        Session["basket"] = Basket_DataTable;

        if (!IsPostBack)
        { // enter code here`enter code here ?!?!
            GridView2.DataSource = Basket_DataTable;
            GridView2.DataBind();
        }
 

    }


对于持久购物车,你需要一些方法来识别回来的用户。下面的两个解决方案都会让购物车重新启动。



您可以通过多种方式实现这一目标,最简单的方法是将购物车产品添加到表格中
购物车

有栏目

cart_id uniqueidentifier

product_id int

数量

价格



然后你将产品添加/删除到该表中,然后将cart_id保存到< a href =http://msdn.microsoft.com/en-us/library/system.web.httpresponse.cookies(v=vs.110).aspx>用户Cookie [ ^ ] - 假设他接受了他们(大多数人都这样做了)。



弱点是Cookie绑定到那个单一设备 - 如果用户通过...笔记本电脑访问你的网店或平板电脑而不是台式电脑,他必须重新开始。此外,如果用户从某些公共计算机(例如互联网咖啡馆计算机库)接受cookie,则下一位用户将访问您的网站。

作为开发人员,您有责任清空推车后删除cookie并将其持续时间设置为合理的时间长度(我通常将其设置为两周。







替代方案和用户要求更高的是强制用户在购买之前在您的网站上注册。这样,当他登录时,您可以检索他的购物车,并且所有内容都针对该特定用户。你可以记录他的购物历史,按照他的订单,他在你的网站上花费的金额,他的喜好等...

这对用户来说很麻烦,你最好有可以保留他们的内容订婚 - 你可以做饼干和这个。对那些打扰你注册的人,你可以提供额外的东西来激励他们。
For persistent cart you need some way to identify the user that comes back. Both solutions below will keep the cart on project restart.

You can do that in several ways, simplest of which is to add the cart products into the table
CART
having columns
cart_id uniqueidentifier
product_id int
quantity
price

then you add/delete the products into that table and you save cart_id into users Cookie[^] - assuming he accepts them (most do).

Weakness is that Cookie is bound to that single device - if the user accesses your web shop through ...laptop or tablet instead of desktop computer, he has to start over. Also, if the user accepts the cookie from some public computer (say library of internet caffe computer) - next user to go to your site will have the cart filled.
It is your responsibility as a developer to delete the cookie after the cart is emptied and to set its duration to some reasonable lenght of time (I usually set it to two weeks.



Alternative and more demanding of the user is to force the user to register on your site before buying. That way, when he logs in, you can retrieve his cart and everything is personalized for that specific user. You can keep history of his shopping, follow his orders, amount he spent on your site, his preferences etc...
This is bothersome to users and you better have the content that can keep them engaged - you can do both cookies and this. To those who bother register you offer something extra to motivate them.


这篇关于将产品添加到会话中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆