/// <summary> /// 根据单号从数据库中删除单据的表头和内容 /// </summary> /// <param name="billNum">单据号</param> private bool DeleteBillByBillNum(string billNum) { //实例化数据适配器 using (var daHead = new t_sample_notice_headTableAdapter()) using (var daContent = new t_sample_notice_contentTableAdapter()) //取得数据库连接 using (var conn = daHead.Connection) { //打开数据库连接 conn.Open(); //设置数据库连接 daContent.Connection = conn; //事务 using (var tran = conn.BeginTransaction()) { //设置事务 daHead.Transaction = daContent.Transaction = tran; try { //执行删除 daHead.Delete(billNum); daContent.DeleteByBillNum(billNum); //提交事务 tran.Commit(); //返回成功 return true; } catch (Exception ex) { //回滚事务 tran.Rollback(); //抛出错误 throw new Exception("删除样板通知单记录出现错误:\n" + ex.Message); } } } }
protected void fvSampleNoticeAdd_ItemCreated(object sender, EventArgs e) { //新增状态 if (fvSampleNoticeAdd.CurrentMode == FormViewMode.Insert) { //当前单据日期文本框 var tb = (TextBox)fvSampleNoticeAdd.FindControl("txtBillDate"); if (tb != null) { //检查用户提供的单据日期参数标识 string strBillDate = Request["bdate"]; DateTime billDate; if (strBillDate != null && strBillDate.Length > 0 && DateTime.TryParse(strBillDate, out billDate)) { //设置默认单据日期为传入的日期 tb.Text = billDate.ToString("yyyy-MM-dd"); } else { //设置默认单据日期为当前日期 tb.Text = DateTime.Now.ToString("yyyy-MM-dd"); } } //数据适配器 using (var da = new t_sample_notice_headTableAdapter()) { //当前单号文本框 tb = (TextBox)fvSampleNoticeAdd.FindControl("txtBillNum"); if (tb != null) { //户提供的单号参数标识 string strBillNum = Request["bnum"]; Int64 iBillNum; //检测单号 if (string.IsNullOrWhiteSpace(strBillNum) || strBillNum.Trim().Length < 13 || !Int64.TryParse(strBillNum.Substring(1), out iBillNum) ) { //获取已经使用的最大单号 strBillNum = da.GetMaxBillNum().ToString(); //检测单号 if (string.IsNullOrWhiteSpace(strBillNum) || strBillNum.Trim().Length < 13 || !Int64.TryParse(strBillNum.Substring(1), out iBillNum) ) { //设置默认单号为传入的单号加1 iBillNum = 0; } } //设置默认单号为传入的单号加1 //单号写入文本框 tb.Text = string.Format("S{0:000000000000}", ++iBillNum); } //设置默认订单序号 tb = (TextBox)fvSampleNoticeAdd.FindControl("txtOrderId"); if (tb != null) { //用户提供的单号参数标识 string strOrder = Request["oid"]; if (!string.IsNullOrWhiteSpace(strOrder)) { //设置值到文本框 tb.Text = strOrder.Trim(); } else { //获取当前已经开样板通知单的最大订单序号 string maxOrderId = da.GetLastOrderId().ToString(); if (string.IsNullOrWhiteSpace(maxOrderId) || maxOrderId.Length < 3) { maxOrderId = "SA0"; } //设置为最大id号+1 tb.Text = maxOrderId.Substring(0, 2) + (int.Parse(maxOrderId.Substring(2)) + 1).ToString(); } } } } //修改状态 else if (fvSampleNoticeAdd.CurrentMode == FormViewMode.Edit) { //遍历控件设置只读与否 var ids = "txtBillDate,txtOrderId,txtRemark".Split(','); foreach (var id in ids) { //设置只读与否 var tb = (TextBox)fvSampleNoticeAdd.FindControl(id); if (tb != null) { tb.Enabled = canEditHeadRow; } } } //当前用户所在部门 string procName = Session["proc_name"].ToString(); //当前角色id Int16 roleId = Convert.ToInt16(Session["role_id"]); //检测是否有权限 if (!(new string[] { "PMC", mustProcName }).Contains(procName) || roleId < 0 || roleId > 4) { //设置编辑按钮不可用 var tb = (Button)fvSampleNoticeAdd.FindControl("btnEdit"); if (tb != null) { tb.Enabled = false; } } if (procName != mustProcName) { //设置添加按钮不可用 var tb = (Button)fvSampleNoticeAdd.FindControl("btnAdd"); if (tb != null) { tb.Enabled = false; } //设置删除按钮不可用 tb = (Button)fvSampleNoticeAdd.FindControl("btnDelete"); if (tb != null) { tb.Enabled = false; } //设置取消按钮不可用 tb = (Button)fvSampleNoticeAdd.FindControl("btnCancelAdd"); if (tb != null) { tb.Enabled = false; } //设置取消按钮不可用 tb = (Button)fvSampleNoticeAdd.FindControl("btnCancelUpdate"); if (tb != null) { tb.Enabled = false; } } //当前订单序号获取焦点 var txtOrderId = (TextBox)fvSampleNoticeAdd.FindControl("txtOrderId"); if (txtOrderId != null) { //设置焦点 txtOrderId.Focus(); } }
/// <summary> /// 执行删除数据并处理相关数据一致性 /// </summary> /// <param name="e">传入的带有数据的事件参数</param> /// <returns></returns> private bool DeleteData(ListViewDeleteEventArgs e) { //实例化数据适配器 using (var daContent = new t_sample_notice_contentTableAdapter()) //取得数据库连接 using (var conn = daContent.Connection) { //打开数据库连接 conn.Open(); //事务 using (var tran = conn.BeginTransaction()) { //设置事务 daContent.Transaction = tran; try { //当前单号 var billNum = Convert.ToString(e.Keys[0]); //当前行号 byte rowId = Convert.ToByte(e.Keys[1]); //根据单号和行号删除记录 if (daContent.Delete(billNum, rowId) <= 0) { throw new Exception("根据单号删除内容记录失败!"); } //检测当前记录内容行数 int? iCount = (int?)daContent.CountByBillNum(billNum); if (iCount.HasValue && iCount.Value <= 0) { //表头适配器 using (var daHead = new t_sample_notice_headTableAdapter()) { //设置事务 daHead.Transaction = daContent.Transaction = tran; //删除表头 if (daHead.Delete(billNum) <= 0) { throw new Exception("根据单号删除表头记录失败!"); } } } //提交事务 tran.Commit(); //返回成功 return true; } catch (Exception ex) { //回滚事务 tran.Rollback(); //抛出错误 throw new Exception("删除入仓单记录出现错误:\n" + ex.Message); } } } }
/// <summary> /// 根据输入的参数值来执行更新数据 /// </summary> /// <param name="e">传入的带有数据的事件参数</param> /// <returns></returns> private bool UpdateData(FormViewUpdateEventArgs e) { //数据适配器 //当前添加语句对象 //当前数据库连接 //数据适配器 using (var daHead = new t_sample_notice_headTableAdapter()) using (var daContent = new t_sample_notice_contentTableAdapter()) using (var conn = daHead.Connection) { //打开数据库连接 conn.Open(); //设置数据库连接 daContent.Connection = conn; //开启事务 using (var tran = conn.BeginTransaction()) { //设置事务 daHead.Transaction = daContent.Transaction = tran; //试运行 try { //当前行单号 string billNum = Convert.ToString(e.Keys[0]); //检测是否需要保存表头 if (canEditHeadRow) { //从单据表头中删除 if (daHead.Delete(billNum) <= 0) { throw new Exception("从单据表头中删除失败!"); } //保存表头 daHead.Insert( Convert.ToDateTime(e.NewValues["bill_date"]), billNum, Convert.ToString(e.NewValues["order_id"]), Convert.ToString(e.NewValues["remark"]), Convert.ToString(e.NewValues["add_person"]), Convert.ToDateTime(e.NewValues["add_time"]), Convert.ToDateTime(e.NewValues["last_change_time"]) ); } //检测是否需要保存表身 if (canEditSonRow) { //从单据内容中删除 if (daContent.DeleteByBillNum(billNum) <= 0) { //throw new Exception("从单据内容中删除失败!"); } //获取表身内容 var rowId = Request.Form.GetValues("row_id"); var materialType = Request.Form.GetValues("material_type"); var materialCode = Request.Form.GetValues("material_code"); var materialName = Request.Form.GetValues("material_name"); var materialSize = Request.Form.GetValues("material_size"); var materialUnit = Request.Form.GetValues("material_unit"); var materialPnlLength = Request.Form.GetValues("material_pnl_length"); var materialPnlWidth = Request.Form.GetValues("material_pnl_width"); var materialPnlCountPcs = Request.Form.GetValues("material_pnl_count_pcs"); var materialPnlQty = Request.Form.GetValues("material_pnl_qty"); //取得表身内保存的行数 var iCountContentRow = 0; //遍历保存表身内容 for (int i = 0; i < rowId.Length; i++) { //有数据才保存 if ( materialType[i].Trim().Length > 0 || materialCode[i].Trim().Length > 0 || materialName[i].Trim().Length > 0 || materialUnit[i].Trim().Length > 0 || materialPnlLength[i].Trim().Length > 0 || materialPnlWidth[i].Trim().Length > 0 || materialPnlCountPcs[i].Trim().Length > 0 || materialPnlQty[i].Trim().Length > 0 ) { //执行保存 daContent.Insert( billNum, Convert.ToByte(rowId[i]), Convert.ToString(materialType[i]), Convert.ToString(materialCode[i]), Convert.ToString(materialName[i]), Convert.ToString(materialSize[i]), Convert.ToString(materialUnit[i]), Convert.ToDecimal(materialPnlLength[i]), Convert.ToDecimal(materialPnlWidth[i]), Convert.ToInt32(materialPnlCountPcs[i]), Convert.ToDecimal(materialPnlQty[i]) ); //累加已保存行数 iCountContentRow++; } } //检测行数 if (iCountContentRow <= 0) { //抛出错误 throw new Exception("单据无内容不能保存!"); } } //提交事务 tran.Commit(); //返回成功 return true; } catch (Exception ex) { //回滚事务 tran.Rollback(); //非数字返回失败 throw new Exception(ex.Message); } } } }