Exemple #1
0
        /// <summary>
        /// 保存
        /// </summary>
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            var table = ViewState["ViewDT"] as DataTable;

            if (table == null || table.Rows.Count == 0)
            {
                return;
            }

            SqlConnection con = LocalGlobal.DbConnect();

            con.Open();
            SqlTransaction tran = con.BeginTransaction();//使用事务
            SqlCommand     cmd  = new SqlCommand();

            cmd.Connection  = con;
            cmd.Transaction = tran;

            var    dateModel = LocalGlobal.GetDateModel();
            string prNo      = string.Empty;

            using (PurProvider purProvider = new PurProvider())
            {
                try
                {
                    if (PRNo.Text.Trim().Length == 0)               //新增
                    {
                        prNo = purProvider.GetMaxPRNo().ToString(); //生成新的请购单号

                        cmd.CommandText = purProvider.InsertPRSql(prNo, "", "", "S", "RMB", "材料请购", LocalGlobal.CurrentUserID, dateModel.DateStr, "", "0");
                        cmd.ExecuteNonQuery();

                        PRStatus.Text = "NE";
                    }
                    else
                    {
                        //修改请购单
                        prNo = PRNo.Text.Trim();

                        //先删除再添加
                        cmd.CommandText = purProvider.DeletePRDetailSql(prNo);
                        cmd.ExecuteNonQuery();

                        //更新状态
                        cmd.CommandText = purProvider.UpdatePRStatusSql(prNo, "UP");
                        cmd.ExecuteNonQuery();

                        PRStatus.Text = "UP";
                    }

                    #region Insert

                    int seq = 0;
                    foreach (DataRow row in table.Rows)
                    {
                        seq++;

                        string prDate = row["PRLPDTE"].ToString().Trim();
                        prDate = LocalGlobal.ConvertDateFormat(prDate).ToString("yyyyMMdd");

                        cmd.CommandText = purProvider.InsertMatPRDetailSql(prNo, seq.ToString(), row["PRLQTY"].ToString().Trim(), prDate, prlwhs.Text.Trim(), row["prltno"].ToString(), row["prlstation"].ToString(), row["prlrule"].ToString(), row["prlum"].ToString(), LocalGlobal.CurrentUserID, dateModel.DateStr, dateModel.TimeStr, row["prlmrk"].ToString(), row["prloutno"].ToString(), row["prlpicno"].ToString());

                        cmd.ExecuteNonQuery();
                    }

                    #endregion
                }
                catch (Exception error)
                {
                    tran.Rollback();
                    this.ShowErrorMessage("保存失败。" + error.Message);
                    return;
                }

                tran.Commit();

                PRNo.Text = prNo;
                this.BindList();
            }
        }