如何将购物车中的值插入数据库 [英] How to insert values from a shopping cart into database

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

问题描述

你好朋友.
我正在开发一个购物车应用程序,其中客户将购买商品,并且所选商品将通过网格视图显示在他的购物车中.
我的疑问是如何将网格视图中的值插入数据库中.
客户购买的所有物品都应插入数据库中.


感谢和问候.

hello friends.
I am developing a shopping cart application in which customer will purchase item and the selected item will be displayed in his shopping cart through grid view.
My doubt is how to insert the values from the grid view into the database.
All the purchased items of customer should be inserted into the database.


thanks and regards.

推荐答案

看一下一些现有的ecommrce代码.例如:

http://dotshoppingcart.codeplex.com/ [ ^ ]

http://nbstore.codeplex.com/ [ ^ ]
take a look at some of the existing ecommrce code. example:

http://dotshoppingcart.codeplex.com/[^]

http://nbstore.codeplex.com/[^]


为什么?您可以使用会话信息(或者如果您的用户必须登录,也可以通过Cookie)从代码中填充gridview.

那么为什么要从写回的地方取回数据并重新处理它,以确保它正确无误,而当您可以返回从其写出的源数据时(无论如何都是DB友好格式),并且用那个代替吗?

[edit] GridView,而不是数据库,Griff:doh:-OriginalGriff [/edit]
Why? You fill the gridview from your code, using the session information (or possibly from cookies if your user must log in).

So why get the data back from where you wrote it and re-process it to make sure that it is correct and unmodified when you could just go back to the source data you wrote it from (which is in a DB friendly format anyway) and use that instead?

[edit]GridView, not Database, Griff :doh: - OriginalGriff[/edit]


使用foreach遍历gridview的值并将它们逐行插入数据库中.

这是给你的例子

use foreach to traverse through the values of the gridview and insert them into the database one row by row.

here is an example for you

protected void btnCheckout_Click(object sender, EventArgs e)
  {
      con.Open();


       foreach(GridViewRow rowno in GridView1.Rows )
       {
           if (rowno.RowType == DataControlRowType.DataRow)
          {
        try
        {
            string productidnow = rowno.Cells[0].Text.ToString();
            int quantitynow = int.Parse(((TextBox)rowno.Cells[2].FindControl("txtQuantity")).Text);
            decimal  pricenow = Convert.ToDecimal(rowno.Cells[3].Text.ToString());
            decimal  totalnow = Convert.ToDecimal(rowno.Cells[4].Text);

            SqlCommand cmd = new SqlCommand("Insert into orders (productid, quantity , price, total) values (@productid,@quantity,@price,@total) ",con);
            cmd.Parameters.AddWithValue("@productid", productidnow );
            cmd.Parameters.AddWithValue("@quantity", quantitynow );
            cmd.Parameters.AddWithValue("@price", pricenow );
            cmd.Parameters.AddWithValue("@total", totalnow );
                  cmd.ExecuteNonQuery();
        }
           catch(Exception ex)
        {
            lblMessage.Text = ex.Message;
            break;

           }
           }
       }
  }



这里的购物车项目位于gridview内.现在,我已经使用foreach循环遍历网格视图中的每一行并将数据添加到数据库中.希望能帮助到你.随时询问您是否不了解代码的任何部分.



here shopping cart items are inside the gridview. Now i have used foreach loop to traverse and add data to database for each row inside the gridview. hope it helps. Feel free to ask if u dont understand any part of the code.


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

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