📄 checkout.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 Checkout : System.Web.UI.Page
{
Class1 db = new Class1();
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
GridView1Bind();
}
}
protected void GridView1Bind()
{
GridView1.DataSource = db.createdataset("select * from ShopCart");
GridView1.DataBind();
//Label4.Text = ShoppingCartTotalCost();
ShoppingCartTotalCost();
}
public void ShoppingCartTotalCost()
{
string sql = "select sum(MoneyCount)as moneycount from ShopCart";
SqlDataReader dr=db.Reader(sql);
string moneycount=null;
while(dr.Read())
{
moneycount =String.Format("{0:c}",dr.GetDecimal(0).ToString());
}
Label4.Text = moneycount;
//
}
//检查金额
public Boolean CheckMoneyCount(Decimal money)
{
string sql = "select * from Users where UserID=" + Convert.ToInt32(Session["UserID"]);
SqlDataReader dr = db.Reader(sql);
decimal temp = 0;
while (dr.Read())
{
temp=dr.GetDecimal(6);
}
if (money > temp)
{
return false;
}
else
{ //余额,在代码中实现跟新,也可以在存储过程中实现,书的数量就是在存储过程中实现
//如果没有审核,应该不扣钱的,只有通过审核了,才减少钱
// string uqdate_money = "update Users set MoneyCount=MoneyCount - " + money + " where UserID=" + Convert.ToInt32(Session["UserID"]);
// db.ExecSql(uqdate_money);
return true;
}
}
//检查图书数量
public Boolean CheckBookCount(int count,int bookid)
{
string sql = "select * from Books where BookID=" + bookid;
SqlDataReader dr = db.Reader(sql);
int temp = 0;
while (dr.Read())
{
temp = Convert.ToInt32(dr["BookAmount"]);
}
if (temp - count < 0)
return false;
else
return true;
}
//这里完成的比较完善
protected void Button2_Click(object sender, EventArgs e)
{
//如果没有登录,则先进行登录,才能进行结算功能
if (Session["UserName"] == null || Session["UserName"].ToString() == "")
{
//2句只执行一句,需的两者结合起来,但是不能给返回上一页那个值中赋值,不能实现上一页功能,就没有用先警告再跳转!!!
// Page.ClientScript.RegisterStartupScript(this.GetType(), "MessageBox", "alert('您还没有登录,请先登录')", true);
//加了下面这一句,弹不出警告对话框
// Response.Redirect("login.aspx");
Response.Redirect("login.aspx");
//Response.Write("<script>alert('您还没有登录,请先登录!');location.href='login.aspx';</script>");
}
else
{
int flag = 0;//标识
//提交,生成订单号,把数据写入订单、详细订单表中,book表中的书本数量要减少,user表中金额总量也要减少
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
int n = Convert.ToInt32(((Label)GridView1.Rows[i].FindControl("Label6")).Text);
//查找每本书的bookid,这里设置了DataKeys为bookid,但是控件没有绑定,不知道会出错没?试试:
//答案:能够找到,哈哈
//在修改book表中数据时,要注意@orderid的赋值给OrderContent的orderid
int key = Convert.ToInt32(GridView1.DataKeys[i].Value);
if (!CheckBookCount(n, key))
break;
else
{
flag++;
continue;
}
}
if (flag < GridView1.Rows.Count)
{
//找出第一个不满足条件的书名,显示在警告对话框中
String name = Convert.ToString(((Label)GridView1.Rows[flag].FindControl("Label7")).Text);
Page.ClientScript.RegisterStartupScript(this.GetType(), "MessageBox", "alert('" + name + " 库存不足,购买失败')", true);
}
else
{
if (CheckMoneyCount(Convert.ToDecimal(Label4.Text.ToString().Trim())))
{
Label3.Text = "<b>尊敬的客户:'" + Convert.ToString(Session["UserName"]) + "', 结算已经完成,请等待核实</b>";
//Label3.Text =Convert.ToString(Session["UserName"]);//(Request.Params["UserName"]);
//Label3.Text = Convert.ToString(Session["UserID"]);
Label5.Text = "你的订单号:" + PlaceOrder();
Label5.Visible = true;
Button2.Visible = false;
//正确的提交之后,应该清空购物车
db.ExecSql("delete from ShopCart");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "MessageBox", "alert('您的余额不够')", true);
}
}
}
}
public int PlaceOrder()
{
int uid = Convert.ToInt32(Session["UserID"]);
// DateTime now = DateTime.Now;
//读取数据库连接字符串
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["BookShopConnectionString"]);
//创建数据库连接对象
SqlConnection myconn = new SqlConnection(settings);
//打开数据库连接
myconn.Open();
SqlCommand myCommand = new SqlCommand("AddOrder", myconn);
//指明Sql命令的操作类型是使用存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//给存储过程添加参数
SqlParameter parameterUserID = new SqlParameter("@UserID", SqlDbType.Int, 4);
parameterUserID.Value = Convert.ToInt32(Session["UserID"]);//Int32.Parse(UserID);
myCommand.Parameters.Add(parameterUserID);
SqlParameter parameterOrderDate = new SqlParameter("@OrderDate", SqlDbType.DateTime, 8);
parameterOrderDate.Value = DateTime.Now;
myCommand.Parameters.Add(parameterOrderDate);
SqlParameter parameterOrderID = new SqlParameter("@OrderID", SqlDbType.Int, 4);
parameterOrderID.Direction = ParameterDirection.Output;
myCommand.Parameters.Add(parameterOrderID);
//进行数据库操作
myCommand.ExecuteNonQuery();
//关闭数据库连接
myconn.Close();
//利用存储过程的OUTPUT参数返回OrderID
return (int)parameterOrderID.Value;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -