public int CreateMoneySum(MoneySum newMoneySum) { int result = -1; try { conn = db.openConn(); tr = conn.BeginTransaction(); sb = new StringBuilder(); sb.Remove(0, sb.Length); sb.Append("INSERT INTO tbMomeySum(psnCode,sumDate,amountwase,amountsum)"); sb.Append(" VALUES (@psnCode,@sumDate,@amountwase,@amountsum)"); string sqlsave; sqlsave = sb.ToString(); comm = new SqlCommand(); comm.Connection = conn; comm.Transaction = tr; comm.CommandText = sqlsave; comm.Parameters.Clear(); comm.Parameters.Add("@psnCode", SqlDbType.NVarChar).Value = newMoneySum.psnCode; comm.Parameters.Add("@sumDate", SqlDbType.NVarChar).Value = newMoneySum.sumDate; comm.Parameters.Add("@amountwase", SqlDbType.NVarChar).Value = newMoneySum.amountwase; comm.Parameters.Add("@amountsum", SqlDbType.NVarChar).Value = newMoneySum.amountsum; comm.ExecuteNonQuery(); tr.Commit(); result = 1; } catch (Exception ex) { tr.Rollback(); conn.Close(); return result; throw ex; } finally { conn.Close(); } return result; }
private void cmdSave_Click(object sender, EventArgs e) { if (txtpsnCode.Text.Trim() == "") { MessageBox.Show("กรุณาเลือกพนักงานก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtpsnCode.Focus(); return; } if (txtamounwase.Text.Trim() == "") { MessageBox.Show("กรุณาป้อนค่าแรง ก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtamounwase.Focus(); return; } MoneySum moneySum = new MoneySum(); moneySum.psnCode = txtpsnCode.Text.Trim(); string _date = String.Format("{0:dd/MM/yyyy}", DateTime.Now); moneySum.sumDate = _date; moneySum.amountwase = txtamounwase.Text.Trim(); moneySum.amountsum = txtamountsum.Text.Trim(); int result = moneySumService.CreateMoneySum(moneySum); if (result > -1) { Console.WriteLine("Insert Complete"); lblresult.Visible = true; lblresult.Text = " บันทึกเรียบร้อย "; } else { Console.WriteLine("Insert Not Complete"); lblresult.Visible = true; lblresult.Text = " ไม่สามารถบันทึกข้อมูลได้"; } }