📄 sell_add.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using LoginCheckClass;
using System.Text;
public partial class 销售管理_Sell_Add : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
LoginCheck a = new LoginCheck(); //对页面进行Session验证,防止非法用户直接通过URL进行登录
a.check();
Pirce.Attributes["onchange"] = Number.Attributes["onchange"] = Label4.Attributes["onchange"] = "javascript:getPrice();"; //引入JavaScript事件
Payment.Attributes["onchange"] = FactPayment.Attributes["onchange"] = "javascript:getPrice1();"; //引入JavaScript事件
if (!IsPostBack)
{
this.Label2.Text = Convert.ToString(Session["username"]); //通过Session获取用户名
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["db_sellConnectionString"].ConnectionString); //连接数据库
SqlCommand cmd = new SqlCommand();
con.Open(); //打开数据库
SqlDataAdapter sda = new SqlDataAdapter("select id,CompanyName from tb_Company where CompanyType='用户'", con); //SQl语句
DataSet ds = new DataSet();
sda.Fill(ds);
ClientName.DataSource = ds;
ClientName.DataTextField = "CompanyName";
ClientName.DataValueField = "id";
ClientName.DataBind();
con.Close(); //关闭数据库
Label3.Text = ClientName.SelectedValue;
CommodityName.DataBind();
}
//抛出异常
catch (Exception ex)
{
Response.Write(@"<script>alert('系统错误')</script>"); //提示出错信息
}
}
}
//实现商品名称和供应商的绑定
protected void CommodityName_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["db_sellConnectionString"].ConnectionString); //连接到数据库
SqlCommand cmd = new SqlCommand();
string id = CommodityName.SelectedValue;
//进行赋值
cmd.CommandText = "SELECT tb_Company.id,tb_Company.CompanyName FROM tb_Commodity INNER JOIN tb_Company ON tb_Commodity.CompanyID = tb_Company.id WHERE tb_Commodity.id =" + id; //SQL语句查询
cmd.Connection = con;
con.Open(); //打开数据库
SqlDataReader reader = cmd.ExecuteReader(); //从数据库中读取数据
//从数据库中读取所需的数据
if (reader.Read())
{
CompanyID.Text = reader["CompanyName"].ToString();
Session["CompanyID"] = reader["id"].ToString();
Session["CommodityID"] =reader["CompanyName"].ToString();
}
getNumber(); //调用方法
}
//商品名的绑定事件
protected void CommodityName_DataBound(object sender, EventArgs e)
{
CommodityName_SelectedIndexChanged(sender, e); //选择商品名称进行绑定
}
//实现数据库的添加功能
protected void AddButton_Click(object sender, EventArgs e)
{
if (StockDate.Text == "")
{
Response.Write(@"<script>alert('请选择销售日期');</script>"); //提示错误信息
}
else
{
string _connectString = ConfigurationManager.ConnectionStrings["db_sellConnectionString"].ConnectionString; //连接数据库
try
{
using (SqlConnection connection = new SqlConnection(_connectString))
{
connection.Open(); //打开数据库
using (SqlCommand command = connection.CreateCommand())
{
command.CommandType = CommandType.Text;
//SQL语句实现数据库的添加
command.Parameters.Add("@CommodityID", SqlDbType.Int).Value = int.Parse(CommodityName.Text);
command.Parameters.Add("@CompanyID", SqlDbType.Int).Value = Session["CompanyID"].ToString();
command.Parameters.Add("@Pirce", SqlDbType.Int).Value = Pirce.Text;
command.Parameters.Add("@Number", SqlDbType.Int).Value = Number.Text;
command.Parameters.Add("@SettlementType", SqlDbType.VarChar, 50).Value = SettlementType.Text;
command.Parameters.Add("@Payment", SqlDbType.Int).Value = Request["Payment"];
command.Parameters.Add("@FactPayment", SqlDbType.Int).Value = FactPayment.Text;
command.Parameters.Add("@NoPayment", SqlDbType.Int).Value = Request["NoPayment"];
command.Parameters.Add("@StockDate", SqlDbType.DateTime).Value = StockDate.Text;
command.Parameters.Add("@ManageMan", SqlDbType.VarChar, 50).Value = ManageMan.Text;
command.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value = Session["UserName"];
command.Parameters.Add("@AddTime", SqlDbType.DateTime).Value = DateTime.Now;
command.Parameters.Add("@ClientID", SqlDbType.Int).Value = ClientName.SelectedValue;
command.Parameters.Add("@Type", SqlDbType.VarChar, 50).Value = "销售信息";
command.CommandText = "Insert into tb_Stock(CommodityID,CompanyID,Pirce,Number,SettlementType,Payment,FactPayment,NoPayment,StockDate,ManageMan,UserName,AddTime,ClientID,Type)Values(@CommodityID,@CompanyID,@Pirce,@Number,@SettlementType,@Payment,@FactPayment,@NoPayment,@StockDate,@ManageMan,@UserName,@AddTime,@ClientID,@Type)";
command.ExecuteNonQuery();
Response.Write(@"<script>alert('添加成功');</script>"); //添加成功时提示“添加成功”信息
getNumber(); //添加成功时,调用方法显示数量的库存
//添加成功时,清空以下内容
Pirce.Text = "";
Payment.Text = "";
FactPayment.Text = "";
NoPayment.Text = "";
ManageMan.Text = "";
}
}
}
//抛出异常
catch (Exception ex)
{
Response.Write(@"<script>alert('系统错误');</script>"); // 添加失败时,提示“系统错误”信息
}
}
}
protected void ClientName_SelectedIndexChanged(object sender, EventArgs e)
{
//Label3.Text = ClientName.SelectedValue;
getNumber(); //调用方法,获得库存信息
}
//getNumber()方法的定义
private void getNumber()
{
using (SqlConnection conn = new SqlConnection(_connectString))
{
conn.Open();
using (SqlCommand command = new SqlCommand())
{
command.Connection = conn;
command.Parameters.Add("@type1", SqlDbType.VarChar, 50).Value = "销售信息";
command.Parameters.Add("@id", SqlDbType.Int).Value = CommodityName.SelectedValue;
command.CommandText = "select isnull(sum(Number),0) from tb_Stock where Type=@type1 and CommodityID=@id";
int saleNumber = Convert.ToInt32(command.ExecuteScalar());
command.Parameters.Add("@type2", SqlDbType.VarChar, 50).Value = "销售退货";
command.CommandText = "select isnull(sum(Number),0) from tb_Stock where Type=@type2 and CommodityID=@id";
int returnNumber = Convert.ToInt32(command.ExecuteScalar());
command.Parameters.Add("@type3", SqlDbType.VarChar, 50).Value = "退货";
command.CommandText = "select isnull(sum(Number),0) from tb_Stock where Type=@type3 and CommodityID=@id";
int sReturnNumber = Convert.ToInt32(command.ExecuteScalar());
command.Parameters.Add("@type4", SqlDbType.VarChar, 50).Value = "进货";
command.CommandText = "select isnull(sum(Number),0) from tb_Stock where Type=@type4 and CommodityID=@id";
int StockNum = (int)command.ExecuteScalar();
int num = StockNum - sReturnNumber - saleNumber + returnNumber;
Number.Text = Convert.ToString(num);
Session["number"] = num;
Label4.Text=Convert.ToString(num);
}
}
}
private static readonly string _connectString = System.Configuration.ConfigurationManager.ConnectionStrings["db_sellConnectionString"].ConnectionString;
protected void Number_TextChanged(object sender, EventArgs e)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -