📄 addtocart.aspx.cs
字号:
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.SqlClient;
public partial class AddTOCart : System.Web.UI.Page
{
Class1 db = new Class1();
int BookID=-1;
protected void Page_Load(object sender, EventArgs e)
{
BookID = Convert.ToInt32(Request.Params["BookID"]);
if (!IsPostBack)
{
//刷屏竟然也会执行这一步,猜测可能是母版的刷新问题
ListAddtoCart();
//返回上一页
if (Request.UrlReferrer != null)
ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();
}
}
protected void ListAddtoCart()
{
//按注销之后,这里不能转换!!!
int uid = Convert.ToInt32(Session["UserID"]);
//未完成,分为登录用户和未登录用户
//未登录用户,可以把书放入购物车,但不能结算,需登录
String insertstr = null;
String updatestr = null;
string sql = null;
if (Session["UserID"] == null || Session["UserID"].ToString() == "")
{
sql = "select * from ShopCart where BookID=" + BookID;
SqlDataReader dr = db.Reader(sql);
if (dr.Read())
{
updatestr = "update ShopCart set BookCount=BookCount+1,MoneyCount=BookPrice*(BookCount+1),IsCanel='0' where BookID=" + BookID;
db.ExecSql(updatestr);
}
else
{
insertstr = "insert into ShopCart( BookID,BookName,BookMarketPrice,BookPrice,ShopDate,BookCount,IsCanel)select BookID,BookName,BookMarketPrice,BookPrice ,GETDATE(),0,0 from Books where BookID= " + BookID;
updatestr = "update ShopCart set BookCount=BookCount+1,MoneyCount=BookPrice*(BookCount+1),IsCanel='0' where BookID=" + BookID;
db.ExecSql(insertstr);
db.ExecSql(updatestr);
}
}
else
{
sql = "select * from ShopCart where UserID=" + uid + " and BookID=" + BookID;
SqlDataReader dr1 = db.Reader(sql);
if (dr1.Read())
{
updatestr = "update ShopCart set BookCount=BookCount+1,MoneyCount=BookPrice*(BookCount+1),IsCanel='0' where UserID=" + uid + " and BookID=" + BookID;
db.ExecSql(updatestr);
}
else
{
insertstr = "insert into ShopCart( BookID,BookName,BookMarketPrice,BookPrice,ShopDate,BookCount,IsCanel,UserID)select BookID,BookName,BookMarketPrice,BookPrice ,GETDATE(),0,0,UserID from Books,Users where UserID=" + uid + " and BookID= " + BookID;
updatestr = "update ShopCart set BookCount=BookCount+1,MoneyCount=BookPrice*(BookCount+1),IsCanel='0' where UserID=" + uid + " and BookID=" + BookID;
db.ExecSql(insertstr);
db.ExecSql(updatestr);
}
}
//怎么查数据库出错!解决,主要是更新和插入解决好
SqlDataReader result = db.Reader(sql);
while (result.Read())
{
Label3.Text = Convert.ToString(result.GetInt32(1));
Label4.Text = result.GetString(2);
Label5.Text = (result.GetDecimal(3)).ToString();
Label6.Text = Convert.ToString(result.GetInt32(5));
Label7.Text = result.GetDecimal(4).ToString();
Label8.Text = Convert.ToString(result.GetDecimal(7));
}
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{//继续购物
//Response.Redirect("Default.aspx");
//返回上一页的功能
Response.Redirect(ViewState["UrlReferrer"].ToString());
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{//结算
Response.Redirect("Checkout.aspx");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -