购物车往mysql里面传值,如何将购物车中的值插入数据库

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.

解决方案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]

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;

}

}

}

}

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值