如何将购物车数据插入另一个表格。 [英] How to insert cart data into another table .

查看:98
本文介绍了如何将购物车数据插入另一个表格。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用列表<>从服务表填充了我的购物车数据类型。现在我想将我选择的服务数据插入订单表。

我的服务页面代码将服务添加到购物车中。请告诉我如何将购物车中的商品添加到订单表?





我的尝试:



这是我的服务页面的代码,我们将服务添加到CArt。

i have populated my cart from the services table by using a list <> data type .now i want to insert my selected service data into the "orders" table .
My services Page code to add service into the cart .please tell me how can we add item in cart to Orders table ?


What I have tried:

this is code of my service page where we add service to CArt.

public List<products> product_list = new List<products>();
   static List<products> cart_list = new List<products>();
   DataAccess da = new DataAccess();
   DataTable dt = new DataTable();
   Login l = new Login();
   protected void Page_Load(object sender, EventArgs e)
   {
       //if (!Page.IsPostBack) ;
       {
           Load_products();
           if (Request.QueryString.AllKeys.Contains("pro_id") && Request.QueryString.AllKeys.Contains("action"))
           {
               string pro_id = Request.QueryString["pro_id"];
               string action = Request.QueryString["action"];
               if (action.Equals("add"))
               {
                   add_to_cart(pro_id);

               }
           }

       }
   }
  /* private void fetchServices()
   {
       da.OpenConnection();
       da.ProcName = "FetchServices";
       da.CreateCommandObject();
       dt = da.FillDataTable();
       DropDownList1.DataSource = dt;
       DropDownList1.DataTextField = "Service_Name";
       DropDownList1.DataValueField = "Service_ID";
       DropDownList1.DataBind();
   }
   */
  void Load_products()
   {
       SqlConnection con = new SqlConnection(@"Data Source=STUDY-BOOK;Initial Catalog=groominglounge;Integrated Security=True");
       con.Open();
       string query = "select * from G_services";
       SqlCommand cmd = new SqlCommand(query, con);
       SqlDataReader reader = cmd.ExecuteReader();
       if (reader.HasRows)
       {
           while (reader.Read())
           {
               product_list.Add(new products(reader["id"].ToString(),reader["Service_Name"].ToString(), reader["Service_price"].ToString(), reader["Service_img"].ToString()));
           }
       }
       con.Close();

   }

   void add_to_cart(string product_id)
   {
       var product = product_list.Where(x=>x.id == product_id);
       foreach(var data in product)
       {
           cart_list.Add(data);
       }
       Session["cart"] = cart_list;
   }

推荐答案

这是我对这个话题的看法。



您有两种选择。一种是使用数据库来存储客户购物车订单。您可能需要存储customerid或标识客户的内容。将该Id与购物车订单一起保存在数据库的临时表中。现在,当客户确认订单时,您可以轻松查询数据库,然后将实际订单保存到最终订单表。



或者,您可以使用Session作为什么在这里演示:会话和购物车 [ ^ ]



如果你问我,我更喜欢选择1使用数据库,因为我们不能真正依赖Sessions来存储数据。为什么?在ASP.NET中,应用程序池经常回收,这通常是我们无法控制的。当发生这种情况时,所有内存状态都将丢失。
Here's my take on this topic.

You have two options to do that. One is to use a database for storing customer cart orders. You may need to store the customerid or something that identifies a customer. Save that Id along with the cart orders in your temporary table in the database. Now when the customer confirms the order, you can easily query your database and then saves the actual order to your final Order table.

Alternatively, you could use Session as what is demonstrated here: Sessions and Shopping Carts[^]

If you were to ask me, I'd prefer option 1 with using database because we can't really rely on Sessions for storing the data. Why is it? In ASP.NET the application pool frequently recycles and this is typically out of our control. When that happened all that in-memory state is lost.


这篇关于如何将购物车数据插入另一个表格。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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