main2.aspx.cs

来自「asp.net+sql2000超市管理系统」· CS 代码 · 共 285 行

CS
285
字号
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;

namespace Supermart
{
	/// <summary>
	/// main2 的摘要说明。
	/// </summary>
	public class main2 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Image Image4;
		protected System.Web.UI.WebControls.Image Image1;
		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.Label lb_backMoney;
		protected System.Web.UI.WebControls.Button btn_complete;
		protected System.Web.UI.WebControls.Image Image5;
		protected System.Web.UI.WebControls.Label lb_ID;
		protected System.Web.UI.WebControls.DropDownList ddl_worker;
		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'2%' 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_mertype();
				Bindddl_merName();
				Bindddl_worker();
				BindmerBit();
				Bind_selllist();
			}
			// 在此处放置用户代码以初始化页面
		}

		#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 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,worker)values('"+this.lb_ID.Text.Trim()+"',GetDate(),'"+this.ddl_worker.SelectedValue+"') ";
					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>");
			}
		}
		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.DataSource=ds.Tables["title"].DefaultView;
			this.selllist.DataBind();
			con.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();
		}

		private void ddl_mertype_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Bindddl_merName();
			BindmerBit();
			this.txt_num.Text="";
			this.lb_Money.Text="0.00";
		}

		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["retailPrice"].ToString();
			sdr.Close();
			con.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();
		}

		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'2%' 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();
			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";
		}

		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;
			this.Bind_selllist();
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?