using ManagementSystem.Utility; using System; using System.Data; using System.Data.SqlClient; using System.Text; using System.Windows.Forms; using System.Drawing; namespace ManagementSystem { public partial class AccountingBookMaintain : Form { //程式內共用物件 public string strFrmStatus = ""; //表單狀態 public string strPKey = ""; //程式內的Key值 string strKey = ""; //程式內加密的Key值 string strActiveUserID = ""; //取得登入作用的使用者帳號 SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID); SqlTransaction sqlTran; SqlDataAdapter sqlAdapter = new SqlDataAdapter(); SqlCommand sqlCmd = new SqlCommand(); DataSet sdInsurance = new System.Data.DataSet(); public AccountingBookMaintain() { InitializeComponent(); } #region 自定義程式 private void StatusChange(string strStatus) //變更主畫面狀態 { strActiveUserID = MainForm.strActiveUserID; strKey = MainForm.strKey; switch (strStatus.ToUpper()) { case "NONE": strFrmStatus = ""; CleanToolbar(); LockForm(); CleanForm(); dgvAccountingBook.ReadOnly = false; break; case "SEARCH": CleanToolbar(); UnLockForm(); tsbOK.Visible = true; tsbCancel.Visible = true; tsbSearch.Visible = false; tsbAdd.Enabled = false; tsbEdit.Enabled = false; tsbDelete.Enabled = false; txtAccountingBookID.Focus(); ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "搜尋"; break; case "ADD": CleanToolbar(); CleanForm(); UnLockForm(); tsbSave.Visible = true; tsbCancel.Visible = true; tsbAdd.Visible = false; tsbSearch.Enabled = false; tsbEdit.Enabled = false; tsbDelete.Enabled = false; txtAccountingBookID.Focus(); ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "新增"; strFrmStatus = "ADD"; break; case "MODIFY": CleanToolbar(); UnLockForm(); txtAccountingBookID.ReadOnly = true; tsbSave.Visible = true; tsbCancel.Visible = true; tsbSearch.Enabled = false; tsbAdd.Enabled = false; tsbEdit.Visible = false; tsbDelete.Enabled = false; txtAccountingBookID.Focus(); ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "修改"; strFrmStatus = "MODIFY"; break; case "DEL": try { if (MessageBox.Show("請問您確定要刪除本資料?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { this.DelEven(); } } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountingBookMaintain.cs", ex); } strFrmStatus = "DEL"; break; } } private void CleanToolbar() //清除工具列 { //設定Toolbar狀態 tsbSearch.Enabled = true; tsbSearch.Visible = true; tsbAdd.Enabled = true; tsbAdd.Visible = true; tsbEdit.Enabled = false; tsbEdit.Visible = false; tsbDelete.Enabled = true; tsbDelete.Visible = true; tsbSave.Visible = false; tsbOK.Visible = false; tsbCancel.Visible = false; } private void GetAccountingBookInfoByID(string strAccountingBookID) { try { //排除空值 if (strAccountingBookID.Trim() == "") { return; } if (strAccountingBookID == null) { return; } strPKey = strAccountingBookID; StringBuilder strSQL = new StringBuilder(""); strSQL.Append("Select * "); strSQL.Append("From OTB_FNC_AccountingBookInfo "); strSQL.Append("Where AccountingBookID = '" + strPKey + "'"); //進行查詢 DataTable dtTemp = (DataTable)UtilityClass.GetSQLResult(strSQL.ToString()).Tables["Result"]; //進行資料顯示 txtAccountingBookID.Text = dtTemp.Rows[0]["AccountingBookID"].ToString(); txtAccountingBookName.Text = dtTemp.Rows[0]["AccountingBookName"].ToString(); cbBelongOrg.SelectedValue = dtTemp.Rows[0]["BelongOrg"].ToString(); cbEffective.Checked = (dtTemp.Rows[0]["Effective"].ToString() == "Y"); ((MainForm)ParentForm).SsStatus.Items["tsslModifyUser"].Text = dtTemp.Rows[0]["ModifyUser"].ToString().Trim(); ((MainForm)ParentForm).SsStatus.Items["tsslModifyDate"].Text = dtTemp.Rows[0]["ModifyDate"].ToString().Trim(); } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountingBookMaintain.cs", ex); } } private void CleanForm() //清除畫面 { string strSQL = "Select AccountingBookID, AccountingBookName From OTB_FNC_AccountingBookInfo"; dgvAccountingBook.DataSource = UtilityClass.GetSQLResult(strSQL).Tables["Result"]; ; cbBelongOrg.DataSource = UtilityClass.GetOrgList().Tables["Organizations"]; //取得清單列表 cbBelongOrg.ValueMember = "OrganizationID"; cbBelongOrg.DisplayMember = "OrganizationName"; txtAccountingBookID.Text = ""; txtAccountingBookName.Text = ""; cbBelongOrg.SelectedIndex = 0; cbEffective.Checked = true; } private bool CheckForm() //確認資料是否填寫正確 { if (txtAccountingBookID.Text.Trim() == "") { MessageBox.Show("請填寫帳本編號", "提示"); txtAccountingBookID.Focus(); return false; } if (txtAccountingBookID.Text.Trim() == "") { MessageBox.Show("請填寫帳本名稱", "提示"); txtAccountingBookName.Focus(); return false; } return true; } private void UnLockForm() //解除限制唯讀物件 { txtAccountingBookID.ReadOnly = false; txtAccountingBookName.ReadOnly = false; cbBelongOrg.Enabled = true; } private void LockForm() //限制唯讀物件 { txtAccountingBookID.ReadOnly = true; txtAccountingBookName.ReadOnly = true; cbBelongOrg.Enabled = false; } private void DelEven() //刪除事件 { StringBuilder sbSQL = new StringBuilder(); try { //執行Delete命令 //執行命令 string strCheckSQL = string.Format("Select AccountingBookID From OTB_FNC_AccountingMaxNumber Where AccountingBookID ='{0}'",strPKey); if (UtilityClass.IsExist(strCheckSQL)) { //帳號資料有被使用 MessageBox.Show("該帳本已被開帳使用,無法刪除!", "提示"); } else { sbSQL.Append("Delete OTB_FNC_AccountingBookInfo Where AccountingBookID = @AccountingBookID"); using (SqlDataAdapter sqlAdapter = new SqlDataAdapter()) { //添加參數 sqlAdapter.DeleteCommand = new SqlCommand(); sqlAdapter.DeleteCommand.Connection = sqlConn; sqlAdapter.DeleteCommand.CommandText = sbSQL.ToString(); sqlAdapter.DeleteCommand.Parameters.AddRange ( new SqlParameter[] { new SqlParameter("@AccountingBookID",txtAccountingBookID.Text.Trim()), } ); if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlAdapter.DeleteCommand.ExecuteNonQuery(); GetAccountingBookInfoByID(txtAccountingBookID.Text.Trim()); //顯示該帳本的值到畫面上 } StatusChange("None"); LockForm(); } } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountingBookMaintain.cs", ex); } } private void AddEvent() //新增事件 { try { if (CheckForm()) { //進行資料新增 StringBuilder strSQL = new StringBuilder(); strSQL.Clear(); strSQL.Append("Insert Into OTB_FNC_AccountingBookInfo(AccountingBookID, AccountingBookName, BelongOrg, Effective, CreateDate, CreateUser, ModifyDate, ModifyUser)"); strSQL.Append("Values (@AccountingBookID, @AccountingBookName, @BelongOrg, @Effective, Getdate(),'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "') "); //添加參數 sqlAdapter.InsertCommand = new SqlCommand(); sqlAdapter.InsertCommand.Connection = sqlConn; sqlAdapter.InsertCommand.CommandText = strSQL.ToString(); sqlAdapter.InsertCommand.Transaction = sqlTran; sqlAdapter.InsertCommand.Parameters.AddRange ( new SqlParameter[] { new SqlParameter("@AccountingBookID",txtAccountingBookID.Text.Trim()), new SqlParameter("@AccountingBookName",txtAccountingBookName.Text.Trim()), new SqlParameter("@BelongOrg",cbBelongOrg.SelectedValue.ToString()), new SqlParameter("@Effective",cbEffective.Checked ? "Y" : "N") } ); if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlAdapter.InsertCommand.ExecuteNonQuery(); MessageBox.Show("資料新增成功", "提示"); dgvAccountingBook.ReadOnly = false; } } catch (Exception ex) { MessageBox.Show("資料新增失敗!", "提示"); ErrorHandler.WriteErrorLog("AccountingBookMaintain.cs", ex); } } private void EditEvent() //修改事件 { try { if (CheckForm()) { //進行資料維護 MessageBox.Show("資料修改成功", "提示"); dgvAccountingBook.ReadOnly = false; } } catch (Exception ex) { MessageBox.Show("資料修改失敗!", "提示"); ErrorHandler.WriteErrorLog("AccountingBookMaintain.cs", ex); } } private void GoEvent() //執行事件 { switch (strFrmStatus) { case "ADD": //新增事件 AddEvent(); StatusChange("NONE"); break; case "MODIFY": //修改事件 EditEvent(); StatusChange("NONE"); break; } } #endregion #region 事件觸發及問題處理 private void AccountingBookMaintain_Load(object sender, EventArgs e) { StatusChange("NONE"); } private void tsbClean_Click(object sender, EventArgs e) { CleanForm(); } private void tsbSearch_Click(object sender, EventArgs e) { StatusChange("SEARCH"); } private void tsbAdd_Click(object sender, EventArgs e) { dgvAccountingBook.ReadOnly = true; StatusChange("ADD"); } private void tsbExit_Click(object sender, EventArgs e) { this.Close(); } private void tsbCancel_Click(object sender, EventArgs e) { StatusChange("NONE"); } private void tsbEdit_Click(object sender, EventArgs e) { dgvAccountingBook.ReadOnly = true; StatusChange("MODIFY"); } private void dgvAccountingBook_CellEnter(object sender, DataGridViewCellEventArgs e) { GetAccountingBookInfoByID(dgvAccountingBook.CurrentRow.Cells[0].Value.ToString()); //顯示該帳本的值到畫面上 } private void tsbSave_Click(object sender, EventArgs e) { GoEvent(); //執行儲存動作 } private void tsbDelete_Click(object sender, EventArgs e) { try { if (MessageBox.Show("請問您確定要刪除本帳本?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { this.DelEven(); } } catch (Exception ex) { ErrorHandler.WriteErrorLog("AccountingBookMaintain.cs", ex); } } #endregion } }