protected void fvMaterialNameAdd_ItemCreated(object sender, EventArgs e) { //修改状态 if (fvMaterialNameAdd.CurrentMode == FormViewMode.Edit) { //当前日期文本框获取焦点 var tb = (TextBox)fvMaterialNameAdd.FindControl("txtMaterialName"); if (tb != null) { //设置焦点 tb.Focus(); } } //新增状态 else if (fvMaterialNameAdd.CurrentMode == FormViewMode.Insert) { //当前文本框 TextBox tb; //获取最大物料代码 using (var da = new t_material_nameTableAdapter()) { //当前物料代码文本框获取焦点 tb = (TextBox)fvMaterialNameAdd.FindControl("txtMaterialCode"); if (tb != null) { //检查用户提供的物料代码参数标识 string materialCode = Request["code"]; if (materialCode == null || materialCode.Length <= 0) { //获取已经使用的最大的代码 materialCode = da.GetMaxCode().ToString(); } if (materialCode == null || materialCode.Length <= 0) { //设置默认物料代码 tb.Text = "A00001"; } else { //设置默认物料代码 tb.Text = string.Format( "{0}{1:00000}", materialCode.Substring(0, 1), (Math.Round(ydPublicMethod.Val(materialCode.Substring(1)), 0) + 1) ); } } } //当前lot卡号文本框获取焦点 tb = (TextBox)fvMaterialNameAdd.FindControl("txtMaterialName"); if (tb != null) { //设置焦点 tb.Focus(); } } }
public void ProcessRequest(HttpContext context) { //清空之前数据 context.Response.Clear(); //检测是否含有session if (context.Session.Count < 5) { //跳转 context.Response.Redirect("/Account/Login", true); //停止加载后续内容 context.Response.End(); //直接返回 return; } //执行基类的方法 if (!base.CheckIsLogin(context)) { return; } //用户传入的参数 string mcode = context.Request["mcode"]; if (mcode == null || mcode.Length <= 0) { throw new Exception("未提供待查询的字母参数mcode!"); } if (mcode.Length != 1) { throw new Exception("提供的字母参数mcode必须为一个字母!"); } //待输出到浏览器的数据 string strResult = string.Empty; //数据适配器 using (var da = new t_material_nameTableAdapter()) { //获取最大物料代码 string materialCode = da.GetMaxCodeLikeMaterialCode(mcode + "%").ToString(); //检测最大的编码是否存在 if (materialCode.Length <= 1) { strResult = mcode + "00001"; } else { strResult = mcode + (int.Parse(materialCode.Substring(1)) + 1).ToString("00000"); } } //写入数据 context.Response.Write(strResult); }
//导出数据到Excel protected void lBtnToExcel_Click(object sender, EventArgs e) { //获取数据并填充到数据表 using (var da = new t_material_nameTableAdapter()) { //检测session中保存的查询条件 object obj = Session["material_name_where_content"]; string str = string.Empty; if (obj != null && obj.ToString().Length > 0) { str = obj.ToString(); } //获取前1万行数据 var tab = da.GetPagedData(0, 10000, str, "order by [material_code] desc"); //不显示单价就把单价清空 if (!isShowPrice) { //遍历数据表 foreach (DataSetMaterialName.t_material_nameRow row in tab.Rows) { //修改值为空 row.SetpriceNull(); } } //设置列标题 string[] titleCol = ("供应商代码,供应商名称,物料代码,物料名称,规格,物料描述," + "单位,单价(元),常用物料,地址,备注,录入员,录入时间,修改时间").Split(','); //设置表名称 string titleTab = "物料名称清单"; //临时文件名称 string fileName = titleTab + DateTime.Now.ToString("yyMMddHHmmss") + ".xls"; //执行导出数据到excel bool isSuccess = ydPublicMethod.DataTable2Excel(tab, titleCol, ref fileName, titleTab); //如果成功则跳转到指定页面 if (isSuccess) { Response.Redirect(fileName); //停止加载后续内容 Response.End(); } } }
/// <summary> /// 检测是否存在指定的物料名称 /// </summary> /// <param name="txtSupplierName">当前供应商名称文本框</param> /// <param name="source">当前验证控件</param> /// <param name="args">验证控件提供的验证事件参数对象</param> /// <returns></returns> internal static bool IsExistMaterialName( TextBox txtSupplierName, object source, ServerValidateEventArgs args ) { //当前对象 var cv = ((CustomValidator)source); cv.ToolTip = "未提供供应商名称"; //是否找到供应商名称文本框 if (txtSupplierName == null) { return false; } //获取用户输入的供应商名称 string supplierName = txtSupplierName.Text; if (supplierName.Length == 0) { return false; } //通过供应商名称与物料名称检测是否存在 using (var da = new t_material_nameTableAdapter()) { //执行获取 int i = (int)da.CountBySupplierNameAndMaterialName(supplierName, args.Value); if (i <= 0) { cv.ToolTip = "根据提供的供应商名称未找到该物料名称"; return false; } else { return true; } } }
/// <summary> /// 根据输入的LOT格式来保存多条数据到数据库 /// </summary> /// <param name="e">传入的带有数据的事件参数</param> /// <returns></returns> private bool InsertData(FormViewInsertEventArgs e) { //日期 DateTime billDate = Convert.ToDateTime(e.Values["bill_date"]); //单号 string billNum = Convert.ToString(e.Values["bill_num"]); //供应商代码 string supplierCode = Convert.ToString(e.Values["supplier_code"]); //供应商名称 string supplierName = Convert.ToString(e.Values["supplier_name"]); //付款方式 string returnWay = Convert.ToString(e.Values["return_way"]); //单据备注 string billRemark = Convert.ToString(e.Values["remark"]); //录入员 string addPerson = e.Values["add_person"].ToString(); //录入时间 DateTime addTime = Convert.ToDateTime(e.Values["add_time"]); //最后修改时间 DateTime lastChangeTime = Convert.ToDateTime(e.Values["last_change_time"]); //数据适配器 //当前添加语句对象 //当前数据库连接 using (var daHead = new t_material_return_headTableAdapter()) using (var daContent = new t_material_return_contentTableAdapter()) using (var daBalance = new t_material_balanceTableAdapter()) using (var daName = new t_material_nameTableAdapter()) using (var daPrice = new t_material_price_historyTableAdapter()) using (var conn = daHead.Connection) { //打开数据库连接 conn.Open(); //开启事务 using (var tran = conn.BeginTransaction()) { //设置数据库连接对象 daContent.Connection = daBalance.Connection = daName.Connection = daPrice.Connection = conn; //设置事务 daHead.Transaction = daContent.Transaction = daBalance.Transaction = daName.Transaction = daPrice.Transaction = tran; //试运行 try { //含有数据的行数 int iCountContent = 0; //遍历子表执行保存 for (int iRow = 0; iRow < 10; iRow++) { //子表各控件 var tr = tabDataListSon.Rows[iRow + 1]; var litRowId = (Literal)tr.Cells[0].Controls[0]; var txtMaterialCode = (TextBox)tr.Cells[1].Controls[0]; var txtMaterialName = (TextBox)tr.Cells[2].Controls[0]; var txtMaterialSize = (TextBox)tr.Cells[3].Controls[0]; var txtQty = (TextBox)tr.Cells[4].Controls[0]; var txtMaterialUnit = (TextBox)tr.Cells[5].Controls[0]; var txtPrice = (TextBox)tr.Cells[6].Controls[0]; var txtRemark = (TextBox)tr.Cells[7].Controls[0]; //取得数据 byte rowId = Convert.ToByte(litRowId.Text); string materialCode = txtMaterialCode.Text; string materialName = txtMaterialName.Text; string materialSize = txtMaterialSize.Text; decimal qty = txtQty.Text.Trim().Length <= 0 ? 0m : decimal.Parse(txtQty.Text.Trim()); string materialUnit = txtMaterialUnit.Text; decimal price = txtPrice.Text.Trim().Length <= 0 ? 0m : decimal.Parse(txtPrice.Text.Trim()); string remark = txtRemark.Text; //存在数据才保存 if (materialCode.Length > 0 && materialName.Length > 0 && materialUnit.Length > 0 && qty > 0) { //从原料结存清单扣数 if (!ydOperateMaterial.DecreaseMaterialBalance( daBalance, BillType.Out, supplierName, materialName, price, qty )) { return false; } //保存到单据内容清单 if (daContent.Insert( billNum, rowId, materialCode, materialName, materialSize, qty, materialUnit, price, remark ) <= 0) { throw new Exception("保存到单据内容清单失败!"); } //含有数据的行数计数 iCountContent++; } } //保存表头 if (iCountContent > 0) { if (daHead.Insert( billDate, billNum, supplierCode, supplierName, returnWay, billRemark, addPerson, addTime, lastChangeTime ) <= 0) { throw new Exception("保存到单据表头清单失败!"); } } //提交事务 tran.Commit(); //返回成功 return true; } catch (Exception ex) { //回滚事务 tran.Rollback(); //非数字返回失败 throw new Exception(ex.Message); } } } }
/// <summary> /// 检查用的输入是否合法.合法返回true,否则返回false /// </summary> /// <returns></returns> private bool CheckUserInput() { //部门序号文本框 var txtProcId = (TextBox)fvProcMaterialBalanceAdd.FindControl("txtProcId"); //数据适配器 using (var daProc = new t_procTableAdapter()) using (var conn = daProc.Connection) { //打开数据连接 conn.Open(); //取得部门相关数据行 var tabProc = daProc.GetDataByProcId(byte.Parse(txtProcId.Text)); //检测部门名称 if (tabProc.Rows.Count <= 0) { throw new Exception(string.Format("没有部门序号为 {0} 的部门!", txtProcId.Text)); } //部门名称文本框 var txtProcName = (TextBox)fvProcMaterialBalanceAdd.FindControl("txtProcName"); //序号和名称必须对应 if (!txtProcName.Text.ToUpper().Equals(((DataSetProc.t_procRow)tabProc.Rows[0]).proc_name.ToUpper())) { throw new Exception(string.Format("部门序号与部门名称不匹配!!", txtProcId.Text)); } //当前数量文本框 var txtQty = (TextBox)fvProcMaterialBalanceAdd.FindControl("txtQty"); //将用输入的内容转换为整数数字 decimal dec; //检测数量文本框 if (!decimal.TryParse(txtQty.Text, out dec) || dec <= 0) { throw new Exception("数量必须大于0!"); } //当前单价文本框 var txtPrice = (TextBox)fvProcMaterialBalanceAdd.FindControl("txtPrice"); //将用输入的内容转换为整数数字 //检测单价文本框 if (!decimal.TryParse(txtPrice.Text, out dec) || dec <= 0) { throw new Exception("单价必须大于0!"); } //结存的物料必须在物料清单中存在且与供应商代码、供应商名称、物料代码、规格、单位、单价一一对应 using (var daMaterial = new t_material_nameTableAdapter()) { //设置数据库连接 daMaterial.Connection = conn; //物料代码文本框 var txtMaterialCode = (TextBox)fvProcMaterialBalanceAdd.FindControl("txtMaterialCode"); //获取该物料代码的物料属性 using (var tabMaterial = daMaterial.GetDataByMaterialCode(txtMaterialCode.Text)) { //检测数据 if (tabMaterial.Rows.Count <= 0) { throw new Exception(string.Format("不存在物料代码为 {0} 的物料!", txtMaterialCode.Text)); } //首行数据 var rowFirst = (DataSetMaterialName.t_material_nameRow)tabMaterial.Rows[0]; //检测物料其他属性 //物料类型 var txtMaterialType = (TextBox)fvProcMaterialBalanceAdd.FindControl("txtMaterialType"); if (!rowFirst.material_type.Equals(txtMaterialType.Text)) { throw new Exception( string.Format( "物料代码为 {0} 的物料类型应该为 \n{1}\n而不是\n{2}!", txtMaterialCode.Text, rowFirst.material_type, txtMaterialType.Text ) ); } //供应商代码 var txtSupplierCode = (TextBox)fvProcMaterialBalanceAdd.FindControl("txtSupplierCode"); if (!rowFirst.supplier_code.Equals(txtSupplierCode.Text)) { throw new Exception( string.Format( "物料代码为 {0} 的物料的供应商代码应该为\n{1}\n而不是\n{2}!", txtMaterialCode.Text, rowFirst.supplier_code, txtSupplierCode.Text ) ); } //供应商名称 var txtSupplierName = (TextBox)fvProcMaterialBalanceAdd.FindControl("txtSupplierName"); if (!rowFirst.supplier_code.Equals(txtSupplierCode.Text)) { throw new Exception( string.Format( "物料代码为 {0} 的物料的供应商名称应该为\n{1}\n而不是\n{2}!", txtMaterialCode.Text, rowFirst.supplier_name, txtSupplierName.Text ) ); } //物料名称 var txtMaterialName = (TextBox)fvProcMaterialBalanceAdd.FindControl("txtMaterialName"); if (!rowFirst.material_name.Equals(txtMaterialName.Text)) { throw new Exception( string.Format( "物料代码为 {0} 的物料名称应该为 \n{1}\n而不是\n{2}!", txtMaterialCode.Text, rowFirst.material_name, txtMaterialName.Text ) ); } //物料规格 var txtMaterialSize = (TextBox)fvProcMaterialBalanceAdd.FindControl("txtMaterialSize"); if (!(rowFirst.Ismaterial_sizeNull() ? string.Empty : rowFirst.material_size).Equals(txtMaterialSize.Text)) { throw new Exception( string.Format( "物料代码为 {0} 的物料规格应该为 \n{1}\n而不是\n{2}!", txtMaterialCode.Text, rowFirst.material_size, txtMaterialSize.Text ) ); } //物料单位 var txtUnit = (TextBox)fvProcMaterialBalanceAdd.FindControl("txtUnit"); if (!rowFirst.unit.Equals(txtUnit.Text)) { throw new Exception( string.Format( "物料代码为 {0} 的物料单位应该为 \n{1}\n而不是\n{2}!", txtMaterialCode.Text, rowFirst.unit, txtUnit.Text ) ); } //物料单价 if (rowFirst.price != decimal.Parse(txtPrice.Text)) { throw new Exception( string.Format( "物料代码为 {0} 的物料单价应该为 \n{1}\n而不是\n{2}!", txtMaterialCode.Text, rowFirst.price, txtPrice.Text ) ); } } } } //可以提交 return true; }
/// <summary> /// 更新或者新增物料名称到物料名称清单表 /// </summary> /// <param name="daName">操作物料名称表的数据适配器</param> /// <param name="materialType">物料类别</param> /// <param name="supplierCode">供应商代码</param> /// <param name="supplierName">供应商名称</param> /// <param name="materialCode">物料代码/param> /// <param name="materialName">物料名称</param> /// <param name="materialSize">物料规格</param> /// <param name="materialUnit">物料单位</param> /// <param name="price">单价</param> /// <param name="addPerson">当前用户名称</param> private void UpdateToMaterialName( t_material_nameTableAdapter daName, string materialType, string supplierCode, string supplierName, ref string materialCode, string materialName, string materialSize, string materialUnit, decimal price, string addPerson ) { //获取当前指定的供应商名称/物料名称/物料规格/单位的物料名称行 var tab = daName.GetSupplierCodeAndMaterialCode( materialType, supplierName, materialName, materialSize, materialUnit ); //检测是否有查询到数据 if (tab.Rows.Count > 0) { //首行数据 var row = (DataSetMaterialName.t_material_nameRow)tab.Rows[0]; //设置取得物料代码 materialCode = row.material_code; //检查单价是否相同 if (price != row.price) { //执行更新单价 //保存到物料名称清单 if (daName.UpdateData( price, addPerson, materialType, supplierCode, supplierName, materialCode, materialName, materialSize, materialUnit ) <= 0) { throw new Exception("执行更新物料名称表中物料单价失败!"); } } //直接返回 return; } //没有查询到数据就执行新增 //获取最大的物料编码 var maxCode = string.Empty; if (materialCode.Trim().Length > 0) { maxCode = materialCode.Substring(0, 1); } if (maxCode.Length > 0) { maxCode = daName.GetMaxCodeLikeMaterialCode(maxCode + "%").ToString(); } if (maxCode.Length <= 0) { maxCode = daName.GetMaxCode().ToString(); } //最大编码如果没有找到 if (maxCode.Length <= 0) { //设置默认物料代码 materialCode = "A00001"; } else { //设置默认物料代码 materialCode = string.Format( "{0}{1:00000}", maxCode.Substring(0, 1), (Math.Round(ydPublicMethod.Val(maxCode.Substring(1)), 0) + 1) ); } //执行添加 if (daName.InsertData( materialType, supplierCode, supplierName, materialCode, materialName, materialSize, materialUnit, price, addPerson ) <= 0) { throw new Exception("新增保存到物料名称清单失败!"); } }
/// <summary> /// 根据输入的参数值来执行更新数据 /// </summary> /// <param name="e">传入的带有数据的事件参数</param> /// <returns></returns> private bool UpdateData(FormViewUpdateEventArgs e) { //数据适配器 //当前添加语句对象 //当前数据库连接 using (var da = new v_material_inTableAdapter()) using (var conn = da.Connection) { //打开数据库连接 conn.Open(); //开启事务 using (var tran = conn.BeginTransaction()) { //设置事务 da.Transaction = tran; //试运行 try { //当前行单号 string billNum = Convert.ToString(e.Keys[0]); //获取数据 using (var tab = da.GetDataByBillNum(billNum)) { //检查是否获取到行 if (tab.Rows.Count == 0) { //显示失败 throw new Exception("当前记录已经被其他用户删除!"); } //数据适配器 using (var daHead = new t_material_in_headTableAdapter()) using (var daContent = new t_material_in_contentTableAdapter()) using (var daBalance = new t_material_balanceTableAdapter()) using (var daName = new t_material_nameTableAdapter()) using (var daPrice = new t_material_price_historyTableAdapter()) { //设置数据库连接 daHead.Connection = daContent.Connection = daBalance.Connection = daName.Connection = daPrice.Connection = conn; //设置事务 daHead.Transaction = daContent.Transaction = daBalance.Transaction = daName.Transaction = daPrice.Transaction = tran; //将之前的单据内容写回 foreach (DataSetMaterialIn.v_material_inRow row in tab.Rows) { //从原料结存清单中扣除 if (!ydOperateMaterial.DecreaseMaterialBalance( daBalance, BillType.In, row.material_type, row.supplier_name, row.material_name, row.price, row.qty )) { return false; } } //从单据内容中删除 if (daContent.DeleteByBillNum(billNum) <= 0) { throw new Exception("从单据内容中删除失败!"); } //从单据表头中删除 if (daHead.Delete(billNum) <= 0) { throw new Exception("从单据表头中删除失败!"); } //日期 DateTime billDate = Convert.ToDateTime(e.NewValues["bill_date"]); //供应商代码 string supplierCode = Convert.ToString(e.NewValues["supplier_code"]); //供应商名称 string supplierName = Convert.ToString(e.NewValues["supplier_name"]); //送货单号 string deliveryNum = Convert.ToString(e.NewValues["delivery_num"]); //单据备注 string billRemark = Convert.ToString(e.NewValues["remark"]); //录入员 string addPerson = e.NewValues["add_person"].ToString(); //录入时间 DateTime addTime = Convert.ToDateTime(e.NewValues["add_time"]); //最后修改时间 DateTime lastChangeTime = Convert.ToDateTime(e.NewValues["last_change_time"]); //含有数据的行数 int iCountContent = 0; //遍历子表执行保存 for (int iRow = 0; iRow < 10; iRow++) { //列号 var iCol = -1; //子表各控件 var tr = tabDataListSon.Rows[iRow + 1]; var litRowId = (Literal)tr.Cells[++iCol].Controls[0]; var txtMaterialType = (TextBox)tr.Cells[++iCol].Controls[0]; var txtMaterialCode = (TextBox)tr.Cells[++iCol].Controls[0]; var txtMaterialName = (TextBox)tr.Cells[++iCol].Controls[0]; var txtMaterialSize = (TextBox)tr.Cells[++iCol].Controls[0]; var txtQty = (TextBox)tr.Cells[++iCol].Controls[0]; var txtMaterialUnit = (TextBox)tr.Cells[++iCol].Controls[0]; var txtPrice = (TextBox)tr.Cells[++iCol].Controls[0]; var txtRemark = (TextBox)tr.Cells[++iCol].Controls[0]; //取得数据 byte rowId = Convert.ToByte(litRowId.Text); string materialType = txtMaterialType.Text; string materialCode = txtMaterialCode.Text; string materialName = txtMaterialName.Text; string materialSize = txtMaterialSize.Text; decimal qty = txtQty.Text.Trim().Length <= 0 ? 0m : decimal.Parse(txtQty.Text.Trim()); string materialUnit = txtMaterialUnit.Text; decimal price = txtPrice.Text.Trim().Length <= 0 ? 0m : decimal.Parse(txtPrice.Text.Trim()); string remark = txtRemark.Text; //存在数据才保存 if (materialCode.Length > 0 && materialName.Length > 0 && materialUnit.Length > 0 && qty > 0) { //保存到原料结存清单 if (daBalance.InsertData( materialType, supplierCode, supplierName, materialCode, materialName, materialSize, qty, materialUnit, price, remark, addPerson, false ) <= 0) { throw new Exception("保存到原料结存清单失败!"); } //保存到单据内容清单 if (daContent.Insert( billNum, rowId, materialType, materialCode, materialName, materialSize, qty, materialUnit, price, remark ) <= 0) { throw new Exception("保存到单据内容清单失败!"); } //保存到物料名称清单 if (daName.UpdateData( price, addPerson, materialType, supplierCode, supplierName, materialCode, materialName, materialSize, materialUnit ) <= 0) { //获取最大的物料编码 var maxCode = string.Empty; if (materialCode.Trim().Length > 0) { maxCode = materialCode.Substring(0, 1); } if (maxCode.Length > 0) { maxCode = daName.GetMaxCodeLikeMaterialCode(maxCode + "%").ToString(); } if (maxCode.Length <= 0) { maxCode = daName.GetMaxCode().ToString(); } //最大编码如果没有找到 if (maxCode.Length <= 0) { //设置默认物料代码 materialCode = "A00001"; } else { //设置默认物料代码 materialCode = string.Format( "{0}{1:00000}", maxCode.Substring(0, 1), (Math.Round(ydPublicMethod.Val(maxCode.Substring(1)), 0) + 1) ); } //执行添加 if (daName.InsertData( materialType, supplierCode, supplierName, materialCode, materialName, materialSize, materialUnit, price, addPerson ) <= 0) { throw new Exception("保存到物料名称清单失败!"); } } //保存到历史单价清单 if (daPrice.UpdateData( price, addPerson, materialType, supplierCode, supplierName, materialCode, materialName, materialSize, materialUnit ) <= 0) { //执行添加 if (daPrice.InsertData( materialType, supplierCode, supplierName, materialCode, materialName, materialSize, materialUnit, price, addPerson ) <= 0) { throw new Exception("保存到物料历史单价清单失败!"); } } //含有数据的行数计数 iCountContent++; } } //保存表头 if (iCountContent > 0) { if (daHead.Insert( billDate, billNum, supplierCode, supplierName, deliveryNum, billRemark, addPerson, addTime, lastChangeTime ) <= 0) { throw new Exception("从单据表头中删除失败!"); } } } } //提交事务 tran.Commit(); //返回成功 return true; } catch (Exception ex) { //回滚事务 tran.Rollback(); //非数字返回失败 throw new Exception(ex.Message); } } } }
/// <summary> /// 根据输入的参数值来执行更新数据 /// </summary> /// <param name="e">传入的带有数据的事件参数</param> /// <returns></returns> private bool UpdateData(FormViewUpdateEventArgs e) { //检测是否更改了需要更新到其他部门的数据内容 if (e.OldValues["price"].Equals(e.NewValues["price"])) { //执行只更新本物料记录 e.Cancel = false; //更新事件继续执行 return false; } //数据适配器 //当前添加语句对象 //当前数据库连接 using (var da = new t_material_nameTableAdapter()) using (var daPrice = new t_material_price_historyTableAdapter()) using (var conn = da.Connection) { //打开数据库连接 conn.Open(); //开启事务 using (var tran = conn.BeginTransaction()) { //设置事务 da.Transaction = tran; daPrice.Transaction = tran; //试运行 try { //取得数据 string supplierCode = e.NewValues["supplier_code"].ToString(); string supplierName = e.NewValues["supplier_name"].ToString(); string materialCode = Convert.ToString(e.Keys[0]); string materialName = e.NewValues["material_name"].ToString(); string materialSize = e.NewValues["material_size"].ToString(); string description = e.NewValues["description"].ToString(); string materialUnit = e.NewValues["unit"].ToString(); decimal price = e.NewValues["price"].ToString().Trim().Length <= 0 ? 0m : decimal.Parse(e.NewValues["price"].ToString().Trim()); bool isMustShow = Convert.ToBoolean(e.NewValues["is_must_show"]); string address = e.NewValues["address"].ToString(); string remark = e.NewValues["remark"].ToString(); string addPerson = e.NewValues["add_person"].ToString(); DateTime addTime = Convert.ToDateTime(e.NewValues["add_time"].ToString()); DateTime lastChangeTime = Convert.ToDateTime(e.NewValues["last_change_time"].ToString()); //执行更新 if (da.Update( supplierCode, supplierName, materialName, materialSize, description, materialUnit, price, isMustShow, address, remark, addPerson, addTime, lastChangeTime, materialCode ) <= 0) { //抛出错误 throw new Exception("更新数据到物料名称清单记录失败!"); } //保存到历史单价清单 if (daPrice.UpdateData( price, addPerson, supplierCode, supplierName, materialCode, materialName, materialSize, materialUnit ) <= 0) { //执行添加 if (daPrice.InsertData( supplierCode, supplierName, materialCode, materialName, materialSize, materialUnit, price, addPerson ) <= 0) { throw new Exception("保存到物料历史单价清单失败!"); } } //提交事务 tran.Commit(); //返回成功 return true; } catch (Exception ex) { //回滚事务 tran.Rollback(); //抛出错误 throw ex; } } } }
/// <summary> /// 根据输入的LOT格式来保存多条数据到数据库 /// </summary> /// <param name="e">传入的带有数据的事件参数</param> /// <returns></returns> private bool InsertData(FormViewInsertEventArgs e) { //数据适配器 //当前添加语句对象 //当前数据库连接 using (var da = new t_material_nameTableAdapter()) using (var daHistory = new t_material_price_historyTableAdapter()) using (var cmd = da.Adapter.InsertCommand) using (var conn = da.Connection) { //打开数据库连接 conn.Open(); //开启事务 using (var tran = conn.BeginTransaction()) { //设置事务 da.Transaction = tran; daHistory.Transaction = tran; //试运行 try { //循环设置参数值 for (int i = 0; i < cmd.Parameters.Count; i++) { //当前参数对象 var par = cmd.Parameters[i]; //设置参数值 par.Value = e.Values[i]; } //执行查询 if (cmd.ExecuteNonQuery() == 0) { //抛出错误 throw new Exception("添加数据到物料名称清单记录失败!"); } //保存到历史记录 if (daHistory.Insert( e.Values["supplier_code"].ToString(), e.Values["supplier_name"].ToString(), e.Values["material_code"].ToString(), e.Values["material_name"].ToString(), e.Values["material_size"].ToString(), e.Values["description"].ToString(), e.Values["unit"].ToString(), e.Values["price"] as decimal?, e.Values["remark"].ToString(), e.Values["add_person"].ToString(), Convert.ToDateTime(e.Values["add_time"]) ) <= 0) { //抛出错误 throw new Exception("写入单价历史记录失败!"); } //提交事务 tran.Commit(); //返回成功 return true; } catch (Exception ex) { //回滚事务 tran.Rollback(); //抛出错误 throw ex; } } } }
/// <summary> /// 执行删除数据并处理相关数据一致性 /// </summary> /// <param name="e">传入的带有数据的事件参数</param> /// <returns></returns> private bool DeleteData(FormViewDeleteEventArgs e) { //当前物料代码 string code = Convert.ToString(e.Keys[0]); //数据适配器 //当前数据库连接 //当前更新语句对象 using (var da = new t_material_nameTableAdapter()) { //试运行 try { //执行删除 da.Delete(code); //返回成功 return true; } catch (Exception ex) { //非数字返回失败 throw new Exception(ex.Message); } } }
/// <summary> /// 检查用的输入是否合法.合法返回true,否则返回false /// </summary> /// <returns></returns> private bool CheckUserInput() { //取得各文本框 var txtSupplierCode = (TextBox)fvMaterialNameAdd.FindControl("txtSupplierCode"); var txtSupplierName = (TextBox)fvMaterialNameAdd.FindControl("txtSupplierName"); var txtMaterialCode = (TextBox)fvMaterialNameAdd.FindControl("txtMaterialCode"); var txtMaterialName = (TextBox)fvMaterialNameAdd.FindControl("txtMaterialName"); //检测文本框是空否为 foreach (var tb in new TextBox[] { txtMaterialCode, txtMaterialName }) { if (tb != null && tb.Text.Trim().Length <= 0) { throw new Exception("物料代码、物料名称都不能为空!"); } } //物料代码格式检测 if (txtMaterialCode != null && !Regex.IsMatch(txtMaterialCode.Text, @"^[A-Z]\d{5}$")) { throw new Exception("必须为1位大写字母开头加5位数字!"); } //非修改状态下 if (fvMaterialNameAdd.CurrentMode != FormViewMode.Edit) { //供应商名称和物料名称 var supplierName = txtSupplierName.Text; var materialName = txtMaterialName.Text; //供应商名称和物料名称不能同时重复 using (var da = new t_material_nameTableAdapter()) { var iCount = (int?)da.CountBySupplierNameAndMaterialName(supplierName, materialName); if (iCount.HasValue && iCount > 0) { throw new Exception(string.Format( " 供应商 {0} 已经存在物料名称{1},不能重复输入!", supplierName, materialName )); } } } //可以提交 return true; }
public void ProcessRequest(HttpContext context) { //清空之前数据 context.Response.Clear(); //检测是否含有session if (context.Session.Count < 5) { //跳转 context.Response.Redirect("/Account/Login", true); //停止加载后续内容 context.Response.End(); //直接返回 return; } //执行基类的方法 if (!base.CheckIsLogin(context)) { return; } //当前用户所在部门 string procName = context.Session["proc_name"].ToString(); //检测部门必须为指定部门 if (procName != mustProcName) { throw new Exception(string.Format("当前部门必须为{0}才能获取物料信息!", mustProcName)); } //当前角色id Int16 roleId = Convert.ToInt16(context.Session["role_id"]); //检测是否有权限 if (roleId >= 0 && roleId <= 5) { //允许显示单价 isShowPrice = true; } //当前使用的查询参数类型 var parField = ParameterType.None; //用户传入的参数 string bType = context.Request["btype"]; if (bType == null || bType.Length <= 0) { throw new Exception("未提供单据类型参数btype!"); } bType = bType.ToLower(); BillType billType; if (bType == "in") { billType = BillType.In; } else if (bType == "out") { billType = BillType.Out; } else if (bType == "lose") { billType = BillType.Lose; } else if (bType == "back") { billType = BillType.Back; } else if (bType == "other") { billType = BillType.Other; } else { throw new Exception("未提供正确的单据类型参数btype!"); } string supplierCode = context.Request["scode"]; string supplierName = context.Request["sname"]; string materialCode = context.Request["mcode"]; string materialName = context.Request["mname"]; string materialSize = context.Request["msize"]; string materialUnit = context.Request["munit"]; //检测物料参数 if (supplierCode != null && supplierCode.Length > 0) { //查询字段类型为供应商代码 parField = ParameterType.SupplierCode; //去掉空格 supplierCode = supplierCode.Trim().Replace("*", "%") + "%"; } else if (supplierName != null && supplierName.Length > 0) { //查询字段类型为供应商名称 parField = ParameterType.SupplierName; //设置参数值 supplierName = "%" + supplierName.Trim().Replace("*", "%") + "%"; } else if (materialCode != null && materialCode.Length > 0) { //查询字段类型为供应商名称物料代码 parField = ParameterType.MaterialCode; //设置参数值 materialCode = materialCode.Trim().Replace("*", "%") + "%"; } else if (materialName != null && materialName.Length > 0) { //查询字段类型为物料名称 parField = ParameterType.MaterialName; //设置参数值 materialName = "%" + materialName.Trim().Replace("*", "%") + "%"; } else if (materialSize != null && materialSize.Length > 0) { //查询字段类型为物料规格 parField = ParameterType.MaterialSize; //设置参数值 materialSize = "%" + materialSize.Trim().Replace("*", "%") + "%"; } else if (materialUnit != null && materialUnit.Length > 0) { //查询字段类型为物料单位 parField = ParameterType.MaterialUnit; //设置参数值 materialUnit = "%" + materialUnit.Trim().Replace("*", "%") + "%"; } //当前未使用特定的三种字段类型之一进行查询则不输出值直接退出 if (parField == ParameterType.None) { return; } //待输出到浏览器的数据 string strResult = string.Empty; //检测单据类型 //入仓单 if (billType == BillType.In || billType == BillType.Other) { //在供应商清单记录中查询 if (parField == ParameterType.SupplierCode || parField == ParameterType.SupplierName) { //将查询到的结果保存到泛型变量中 var lSupplier = new List<SupplierLiItem>(); //数据适配器 using (var da = new t_material_supplierTableAdapter()) { //获取数据使用的表 DataSetMaterialSupplier.t_material_supplierDataTable tab; //通过代码查询 if (parField == ParameterType.SupplierCode) { //取得数据 tab = da.GetDataLikeSupplierCode(supplierCode); } //通过名称查询 else { //取得数据 tab = da.GetDataLikeSupplierName(supplierName); } //获取单据数据 AddSupplierListItem(tab, ref lSupplier); } //如果在清单中查询到数据 if (lSupplier.Count > 0) { //将泛型变量各项目放入数组 var itms = new SupplierLiItem[lSupplier.Count]; //复制泛型变量的内容到数组 lSupplier.CopyTo(itms); //循环重新设置单只数 foreach (var itm in itms) { //将实例加入li strResult += itm.ToString(); } } } else { //在物料清单中查询 //将查询到的结果保存到泛型变量中 var lMaterial = new List<MaterialLiItem>(); //在物料结存清单中查询 if (billType == BillType.In) { //数据适配器 using (var da = new t_material_balanceTableAdapter()) { //获取数据使用的表 DataSetMaterialBalance.t_material_balanceDataTable tab; //通过供应商代码查询 if (parField == ParameterType.SupplierCode) { //取得数据 tab = da.GetDataLikeSupplierCode(supplierCode); } //通过供应商名称查询 else if (parField == ParameterType.SupplierName) { //取得数据 tab = da.GetDataLikeSupplierName(supplierName); } //通过物料代码查询 else if (parField == ParameterType.MaterialCode) { //取得数据 tab = da.GetDataLikeMaterialCode(materialCode); } //通过物料名称查询 else if (parField == ParameterType.MaterialName) { //取得数据 tab = da.GetDataLikeMaterialName(materialName); } //通过物料规格查询 else if (parField == ParameterType.MaterialSize) { //取得数据 tab = da.GetDataLikeMaterialSize(materialSize); } //通过物料名称查询 else if (parField == ParameterType.MaterialUnit) { //取得数据 tab = da.GetDataLikeMaterialUnit(materialUnit); } else { throw new Exception("检测到错误的参数名称类型!"); } //获取单据数据 AddMaterialListItem(tab, billType, ref lMaterial); } } //在物料清单中查询 //数据适配器 using (var da = new t_material_nameTableAdapter()) { //获取数据使用的表 DataSetMaterialName.t_material_nameDataTable tab; //通过供应商代码查询 if (parField == ParameterType.SupplierCode) { //取得数据 tab = da.GetDataLikeSupplierCode(supplierCode); } //通过供应商名称查询 else if (parField == ParameterType.SupplierName) { //取得数据 tab = da.GetDataLikeSupplierName(supplierName); } //通过物料代码查询 else if (parField == ParameterType.MaterialCode) { //取得数据 tab = da.GetDataLikeMaterialCode(materialCode); } //通过物料名称查询 else if (parField == ParameterType.MaterialName) { //取得数据 tab = da.GetDataLikeMaterialName(materialName); } //通过物料规格查询 else if (parField == ParameterType.MaterialSize) { //取得数据 tab = da.GetDataLikeMaterialSize(materialSize); } //通过物料名称查询 else if (parField == ParameterType.MaterialUnit) { //取得数据 tab = da.GetDataLikeMaterialUnit(materialUnit); } else { throw new Exception("检测到错误的参数名称类型!"); } //获取单据数据 AddMaterialListItem(tab, billType, ref lMaterial); } //如果在清单中查询到数据 if (lMaterial.Count > 0) { //将泛型变量各项目放入数组 var itms = new MaterialLiItem[lMaterial.Count]; //复制泛型变量的内容到数组 lMaterial.CopyTo(itms); //循环重新设置单只数 foreach (var itm in itms) { //将实例加入li strResult += itm.ToString(); } } } } //出仓单 //盘亏单 else if (billType == BillType.Out || billType == BillType.Lose) { //将查询到的结果保存到泛型变量中 var lMaterial = new List<MaterialLiItem>(); //在物料结存清单中查询 //数据适配器 using (var da = new t_material_balanceTableAdapter()) { //获取数据使用的表 DataSetMaterialBalance.t_material_balanceDataTable tab; //通过供应商代码查询 if (parField == ParameterType.SupplierCode) { //取得数据 tab = da.GetDataLikeSupplierCode(supplierCode); } //通过供应商名称查询 else if (parField == ParameterType.SupplierName) { //取得数据 tab = da.GetDataLikeSupplierName(supplierName); } //通过物料代码查询 else if (parField == ParameterType.MaterialCode) { //取得数据 tab = da.GetDataLikeMaterialCode(materialCode); } //通过物料名称查询 else if (parField == ParameterType.MaterialName) { //取得数据 tab = da.GetDataLikeMaterialName(materialName); } //通过物料规格查询 else if (parField == ParameterType.MaterialSize) { //取得数据 tab = da.GetDataLikeMaterialSize(materialSize); } //通过物料名称查询 else if (parField == ParameterType.MaterialUnit) { //取得数据 tab = da.GetDataLikeMaterialUnit(materialUnit); } else { throw new Exception("检测到错误的参数名称类型!"); } //获取单据数据 AddMaterialListItem(tab, billType, ref lMaterial); } //在物料清单中查询 //数据适配器 using (var da = new t_material_nameTableAdapter()) { //获取数据使用的表 DataSetMaterialName.t_material_nameDataTable tab; //通过供应商代码查询 if (parField == ParameterType.SupplierCode) { //取得数据 tab = da.GetDataLikeSupplierCode(supplierCode); } //通过供应商名称查询 else if (parField == ParameterType.SupplierName) { //取得数据 tab = da.GetDataLikeSupplierName(supplierName); } //通过物料代码查询 else if (parField == ParameterType.MaterialCode) { //取得数据 tab = da.GetDataLikeMaterialCode(materialCode); } //通过物料名称查询 else if (parField == ParameterType.MaterialName) { //取得数据 tab = da.GetDataLikeMaterialName(materialName); } //通过物料规格查询 else if (parField == ParameterType.MaterialSize) { //取得数据 tab = da.GetDataLikeMaterialSize(materialSize); } //通过物料名称查询 else if (parField == ParameterType.MaterialUnit) { //取得数据 tab = da.GetDataLikeMaterialUnit(materialUnit); } else { throw new Exception("检测到错误的参数名称类型!"); } //获取单据数据 AddMaterialListItem(tab, billType, ref lMaterial); } //如果在清单中查询到数据 if (lMaterial.Count > 0) { //将泛型变量各项目放入数组 var itms = new MaterialLiItem[lMaterial.Count]; //复制泛型变量的内容到数组 lMaterial.CopyTo(itms); //循环重新设置单只数 foreach (var itm in itms) { //将实例加入li strResult += itm.ToString(); } } } //盘盈单 else if (billType == BillType.Back) { //在物料盘亏清单中查询 //将查询到的结果保存到泛型变量中 var lMaterial = new List<MaterialLiItem>(); //数据适配器 using (var da = new t_material_lose_contentTableAdapter()) { //获取数据使用的表 DataSetMaterialLose.t_material_lose_contentDataTable tab; //通过供应商代码查询 if (parField == ParameterType.SupplierCode) { //取得数据 tab = da.GetDataLikeSupplierCode(supplierCode); } //通过供应商名称查询 else if (parField == ParameterType.SupplierName) { //取得数据 tab = da.GetDataLikeSupplierName(supplierName); } //通过物料代码查询 else if (parField == ParameterType.MaterialCode) { //取得数据 tab = da.GetDataLikeMaterialCode(materialCode); } //通过物料名称查询 else if (parField == ParameterType.MaterialName) { //取得数据 tab = da.GetDataLikeMaterialName(materialName); } //通过物料规格查询 else if (parField == ParameterType.MaterialSize) { //取得数据 tab = da.GetDataLikeMaterialSize(materialSize); } //通过物料名称查询 else if (parField == ParameterType.MaterialUnit) { //取得数据 tab = da.GetDataLikeMaterialUnit(materialUnit); } else { throw new Exception("检测到错误的参数名称类型!"); } //获取单据数据 AddMaterialListItem(tab, billType, ref lMaterial); } //在物料清单中查询 //数据适配器 using (var da = new t_material_nameTableAdapter()) { //获取数据使用的表 DataSetMaterialName.t_material_nameDataTable tab; //通过供应商代码查询 if (parField == ParameterType.SupplierCode) { //取得数据 tab = da.GetDataLikeSupplierCode(supplierCode); } //通过供应商名称查询 else if (parField == ParameterType.SupplierName) { //取得数据 tab = da.GetDataLikeSupplierName(supplierName); } //通过物料代码查询 else if (parField == ParameterType.MaterialCode) { //取得数据 tab = da.GetDataLikeMaterialCode(materialCode); } //通过物料名称查询 else if (parField == ParameterType.MaterialName) { //取得数据 tab = da.GetDataLikeMaterialName(materialName); } //通过物料规格查询 else if (parField == ParameterType.MaterialSize) { //取得数据 tab = da.GetDataLikeMaterialSize(materialSize); } //通过物料名称查询 else if (parField == ParameterType.MaterialUnit) { //取得数据 tab = da.GetDataLikeMaterialUnit(materialUnit); } else { throw new Exception("检测到错误的参数名称类型!"); } //获取单据数据 AddMaterialListItem(tab, billType, ref lMaterial); } //如果在清单中查询到数据 if (lMaterial.Count > 0) { //将泛型变量各项目放入数组 var itms = new MaterialLiItem[lMaterial.Count]; //复制泛型变量的内容到数组 lMaterial.CopyTo(itms); //循环重新设置单只数 foreach (var itm in itms) { //将实例加入li strResult += itm.ToString(); } } } else { throw new Exception("提供的单据类型错误!"); } //加入ul头尾 strResult = "<ul>\n" + strResult + "</ul>\n"; //写入数据 context.Response.Write(strResult); }