📄 shopping.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace shopping.受保护窗体
{
/// <summary>
/// shopping1 的摘要说明。
/// </summary>
public class shopping1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.HyperLink HyperLink2;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
Label1.Text=Session["username"].ToString()+"你好!欢迎来到快乐购物中心!";
DataGrid();
}
}
private void DataGrid()
{
string str="Provider=Microsoft.Jet.OleDb.4.0;";
str+=@"Data Source=";
str+=Server.MapPath("usergoods.mdb");
OleDbConnection conn=new OleDbConnection(str);
conn.Open();
string strsql="select * from goods order by 商品号";
OleDbCommand comm=new OleDbCommand(strsql,conn);
comm.CommandType=CommandType.Text;
OleDbDataAdapter oda=new OleDbDataAdapter();
oda.SelectCommand=comm;
DataTable da= new DataTable("goods");
DataSet ds=new DataSet();
DataTableCollection dc=ds.Tables;
dc.Add(da);
oda.Fill(da);
DataView dv=ds.Tables["goods"].DefaultView;
DataGrid1.DataSource=dv;
DataGrid1.DataBind();
conn.Close();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string str="Provider=Microsoft.Jet.OleDb.4.0;";
str+=@"Data Source=";
str+=Server.MapPath("usergoods.mdb");
OleDbConnection conn=new OleDbConnection(str);
conn.Open();
string goodID=e.Item.Cells[0].Text;
string strsql="select count(*) from shopping where 客户名='"+Session["username"].ToString()+"' and 商品ID='"+goodID+"'";
OleDbCommand comm=new OleDbCommand(strsql,conn);
comm.CommandType=CommandType.Text;
int num=(int)comm.ExecuteScalar();
comm.CommandText="select 数量 from goods where 商品号='"+goodID+"'";
int num2=(int)comm.ExecuteScalar();
if(num2==0)
{
Response.Write("<script language=javascript>alert('该商品暂时缺货!请选择其它的商品!')</script>");
conn.Close();
}
else
{
if(num!=0)
{
comm.CommandText="select 数量 from shopping where 客户名='"+Session["username"].ToString()+"' and 商品ID='"+goodID+"'";
int num1=(int)comm.ExecuteScalar();
comm.CommandText="select 应付金额 from shopping where 客户名='"+Session["username"].ToString()+"' and 商品ID='"+goodID+"'";
int count=(int)comm.ExecuteScalar();
count+=int.Parse(e.Item.Cells[2].Text);
num1++;
comm.CommandText="update shopping set 数量="+num1+",应付金额="+count+" where 客户名='"+Session["username"].ToString()+"' and 商品ID='"+goodID+"'";
comm.ExecuteNonQuery();
}
else
{
comm.CommandText="select 供应商 from Mygoods where 商品ID='"+goodID+"'";
comm.CommandText="insert into shopping values('"+Session["username"].ToString()+"','"+goodID+"','"+comm.ExecuteScalar().ToString()+"','"+e.Item.Cells[1].Text+"',1,'"+e.Item.Cells[2].Text+"',0)";
comm.ExecuteNonQuery();
}
num2--;
comm.CommandText="update goods set 数量="+num2.ToString()+" where 商品号='"+goodID+"'";
comm.ExecuteNonQuery();
conn.Close();
Response.Redirect("Mygood.aspx");
}
}
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
DataGrid();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -