📄 c_body.ascx.cs
字号:
namespace Supermart
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;
/// <summary>
/// C_body 的摘要说明。
/// </summary>
public class C_body : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DropDownList ddl_worker;
protected System.Web.UI.WebControls.TextBox txt_remark;
protected System.Web.UI.WebControls.DropDownList ddl_mertype;
protected System.Web.UI.WebControls.DropDownList ddl_merName;
protected System.Web.UI.WebControls.TextBox txt_num;
protected System.Web.UI.WebControls.Label lb_merBit;
protected System.Web.UI.WebControls.TextBox txt_price;
protected System.Web.UI.WebControls.Label lb_Money;
protected System.Web.UI.WebControls.Button btn_putin;
protected System.Web.UI.WebControls.DataGrid selllist;
protected System.Web.UI.WebControls.Label lb_number;
protected System.Web.UI.WebControls.Label lb_total;
protected System.Web.UI.WebControls.TextBox txt_gathering;
protected System.Web.UI.WebControls.Button btn_complete;
protected System.Web.UI.WebControls.Label lb_backMoney;
protected System.Web.UI.WebControls.Label lb_ID;
protected System.Web.UI.WebControls.DropDownList ddl_buyerName;
private string strConn=System.Configuration.ConfigurationSettings.AppSettings["SQLCONNECTION"];
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
SqlConnection con=new SqlConnection(strConn);
con.Open();
SqlCommand cmd=new SqlCommand("select top 1 * from selllist where selllistID like'1%' order by selllistID desc",con);
SqlDataReader sdr=cmd.ExecuteReader();
sdr.Read();
this.lb_ID.Text=sdr["selllistID"].ToString();
int i=int.Parse(this.lb_ID.Text)+1;
this.lb_ID.Text=i.ToString();
sdr.Close();
con.Close();
Bindddl_buyerName();
Bindddl_worker();
Bindddl_mertype();
Bindddl_merName();
BindmerBit();
Bind_selllist();
}
// 在此处放置用户代码以初始化页面
}
public void Bindddl_buyerName()
{
SqlConnection con=new SqlConnection(strConn);
con.Open();
SqlCommand cmd=new SqlCommand("select * from buyer",con);
SqlDataReader sdr=cmd.ExecuteReader();
this.ddl_buyerName.DataSource=sdr;
this.ddl_buyerName.DataTextField="buyerName";
this.ddl_buyerName.DataValueField="buyerID";
this.ddl_buyerName.DataBind();
con.Close();
sdr.Close();
}
public void Bindddl_provderName()
{
SqlConnection con=new SqlConnection(strConn);
con.Open();
SqlCommand cmd=new SqlCommand("select * from buyer",con);
SqlDataReader sdr=cmd.ExecuteReader();
this.ddl_buyerName.DataSource=sdr;
this.ddl_buyerName.DataTextField="buyerName";
this.ddl_buyerName.DataValueField="buyerID";
this.ddl_buyerName.DataBind();
con.Close();
sdr.Close();
}
public void Bindddl_worker()
{
SqlConnection con=new SqlConnection(strConn);
con.Open();
SqlCommand cmd=new SqlCommand("select * from personnel",con);
SqlDataReader sdr=cmd.ExecuteReader();
this.ddl_worker.DataSource=sdr;
this.ddl_worker.DataTextField="relName";
this.ddl_worker.DataValueField="perID";
this.ddl_worker.DataBind();
con.Close();
sdr.Close();
}
public void Bindddl_mertype()
{
SqlConnection con=new SqlConnection(strConn);
con.Open();
SqlCommand cmd=new SqlCommand("select * from type",con);
SqlDataReader sdr=cmd.ExecuteReader();
this.ddl_mertype.DataSource=sdr;
this.ddl_mertype.DataTextField="typeName";
this.ddl_mertype.DataValueField="typeID";
this.ddl_mertype.DataBind();
con.Close();
sdr.Close();
}
public void Bindddl_merName()
{
SqlConnection con=new SqlConnection(strConn);
con.Open();
SqlCommand cmd=new SqlCommand("select * from merchandise where typeID='"+this.ddl_mertype.SelectedValue+"'",con);
SqlDataReader sdr=cmd.ExecuteReader();
this.ddl_merName.DataSource=sdr;
this.ddl_merName.DataTextField="merName";
this.ddl_merName.DataValueField="merID";
this.ddl_merName.DataBind();
con.Close();
sdr.Close();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ddl_mertype.SelectedIndexChanged += new System.EventHandler(this.ddl_mertype_SelectedIndexChanged);
this.ddl_merName.SelectedIndexChanged += new System.EventHandler(this.ddl_merName_SelectedIndexChanged);
this.txt_num.TextChanged += new System.EventHandler(this.txt_num_TextChanged);
this.btn_putin.Click += new System.EventHandler(this.btn_putin_Click);
this.selllist.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.selllist_PageIndexChanged);
this.selllist.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.selllist_ItemDataBound);
this.txt_gathering.TextChanged += new System.EventHandler(this.txt_gathering_TextChanged);
this.btn_complete.Click += new System.EventHandler(this.btn_complete_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void ddl_mertype_SelectedIndexChanged(object sender, System.EventArgs e)
{
Bindddl_merName();
BindmerBit();
this.txt_num.Text="";
this.lb_Money.Text="0.00";
}
public void Bind_selllist()
{
SqlConnection con=new SqlConnection(strConn);
con.Open();
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand=new SqlCommand("select * from selltable where selllistID='"+this.lb_ID.Text+"'",con);
DataSet ds=new DataSet();
sda.Fill(ds,"title");
sda.Dispose();
this.selllist.DataKeyField="ID";
this.selllist.DataSource=ds.Tables["title"].DefaultView;
this.selllist.DataBind();
con.Close();
}
private void btn_putin_Click(object sender, System.EventArgs e)
{
if(this.txt_num.Text!="")
{
SqlConnection con=new SqlConnection(strConn);
con.Open();
SqlCommand scmd=new SqlCommand("select count(*) from selltable where selllistID='"+this.lb_ID.Text+"'",con);
int count=Convert.ToInt32(scmd.ExecuteScalar());
if(count<1)
{
string my2cmd="insert into selllist(selllistID,selldate,buyer,worker,remark)values('"+this.lb_ID.Text.Trim()+"',GetDate(),'"+this.ddl_buyerName.SelectedValue+"','"+this.ddl_worker.SelectedValue+"','"+this.txt_remark.Text.Trim()+"') ";
SqlCommand ccmd=new SqlCommand(my2cmd,con);
ccmd.ExecuteNonQuery();
}
else
{
SqlCommand acmd=new SqlCommand("update selllist set selldate=GetDate()",con);
acmd.ExecuteNonQuery();
}
string mycmd="insert into selltable(selllistID,merName,num,merBit,price,buyMoney)values('"+this.lb_ID.Text.Trim()+"','"+this.ddl_merName.SelectedItem.Text.Trim()+"','"+this.txt_num.Text.Trim()+"','"+this.lb_merBit.Text.Trim()+"','"+this.txt_price.Text.Trim()+"','"+this.lb_Money.Text.Trim()+"')";
SqlCommand cmd=new SqlCommand(mycmd,con);
cmd.ExecuteNonQuery();
SqlCommand cmdnum=new SqlCommand("select * from merchandise where merID='"+this.ddl_merName.SelectedValue+"'",con);
SqlDataReader sdr=cmdnum.ExecuteReader();
sdr.Read();
int stock=int.Parse(sdr["stock"].ToString());
int num=int.Parse(this.txt_num.Text);
int stock2=stock-num;
sdr.Close();
SqlCommand update=new SqlCommand("update merchandise set stock='"+stock2+"' where merID='"+this.ddl_merName.SelectedValue+"'",con);
update.ExecuteNonQuery();
sdr.Close();
SqlCommand cmdd=new SqlCommand("select sum(Convert(float,buyMoney))as total,sum(Convert(int,num))as number from selltable where selllistID='"+this.lb_ID.Text+"'",con);
SqlDataReader sdr1=cmdd.ExecuteReader();
sdr1.Read();
this.lb_total.Text=sdr1["total"].ToString();
this.lb_number.Text=sdr1["number"].ToString();
sdr1.Close();
Bind_selllist();
con.Close();
}
else
{
Response.Write("<script>alert(\"数量不能为空!!\");</script>");
}
}
private void ddl_merName_SelectedIndexChanged(object sender, System.EventArgs e)
{
BindmerBit();
this.txt_num.Text="";
this.lb_Money.Text="0.00";
}
public void BindmerBit()
{
SqlConnection con=new SqlConnection(strConn);
con.Open();
SqlCommand cmd=new SqlCommand("select * from merchandise where merID='"+this.ddl_merName.SelectedValue+"'",con);
SqlDataReader sdr=cmd.ExecuteReader();
sdr.Read();
this.lb_merBit.Text=sdr["merBit"].ToString();
this.txt_price.Text=sdr["tradePrice"].ToString();
sdr.Close();
con.Close();
}
private void btn_complete_Click(object sender, System.EventArgs e)
{
SqlConnection con=new SqlConnection(strConn);
con.Open();
SqlCommand acmd=new SqlCommand("update selllist set selldate=GetDate() where selllistID='"+this.lb_ID.Text+"'",con);
acmd.ExecuteNonQuery();
SqlCommand cmd=new SqlCommand("select top 1 * from selllist where selllistID like'1%' order by selllistID desc",con);
SqlDataReader sdr=cmd.ExecuteReader();
sdr.Read();
this.lb_ID.Text=sdr["selllistID"].ToString();
int i=int.Parse(this.lb_ID.Text)+1;
this.lb_ID.Text=i.ToString();
sdr.Close();
SqlCommand cmd1=new SqlCommand("select * from buyer where buyerName='"+this.ddl_buyerName.SelectedItem.Text+"'",con);
SqlDataReader sdr1=cmd1.ExecuteReader();
sdr1.Read();
int time=int.Parse(sdr1["buyTime"].ToString())+1;
float totalMoney=float.Parse(sdr1["totalMoney"].ToString())+float.Parse(this.lb_total.Text);
sdr1.Close();
SqlCommand cmd2=new SqlCommand("update buyer set buyTime='"+time+"',totalMoney='"+totalMoney+"',latestBuy=GetDate() where buyerName='"+this.ddl_buyerName.SelectedItem.Text+"'",con);
cmd2.ExecuteNonQuery();
con.Close();
Bind_selllist();
this.lb_total.Text="0.00";
this.lb_number.Text="0";
this.txt_gathering.Text="";
this.lb_backMoney.Text="0.00";
this.txt_num.Text="";
this.lb_Money.Text="0.00";
this.txt_remark.Text="";
}
private void txt_num_TextChanged(object sender, System.EventArgs e)
{
int a=int.Parse(this.txt_num.Text);
float b=float.Parse(this.txt_price.Text);
double c=a*b;
double T=(double)Math.Round(c,2);
this.lb_Money.Text=T.ToString();
}
private void txt_gathering_TextChanged(object sender, System.EventArgs e)
{
float a=float.Parse(this.txt_gathering.Text);
float b=float.Parse(this.lb_total.Text);
if(a>b)
{
double c=a-b;
double T=(double)Math.Round(c,2);
this.lb_backMoney.Text=T.ToString();
}
else
{
Response.Write("<script>alert(\"付款金额不够!!\");</script>");
this.lb_backMoney.Text="0.00";
this.txt_gathering.Text="";
}
}
private void selllist_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c");
}
}
private void selllist_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.selllist.CurrentPageIndex=e.NewPageIndex;
Bind_selllist();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -