public int CreateMillin(Millin newMillin) { int result = -1; try { conn = db.openConn(); tr = conn.BeginTransaction(); sb = new StringBuilder(); sb.Remove(0, sb.Length); sb.Append("INSERT INTO tbMillin(psnCode,cause,mFormat,note,mdateStart,mhurStart,mdateEnd,mhurEnd,mdateAll,mhurAll,mless,mtime)"); sb.Append(" VALUES (@psnCode,@cause,@mFormat,@note,@mdateStart,@mhurStart,@mdateEnd,@mhurEnd,@mdateAll,@mhurAll,@mless,@mtime)"); 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 = newMillin.psnCode; comm.Parameters.Add("@cause", SqlDbType.NVarChar).Value = newMillin.cause; comm.Parameters.Add("@mFormat", SqlDbType.NVarChar).Value = newMillin.mFormat; comm.Parameters.Add("@note", SqlDbType.NVarChar).Value = newMillin.note; comm.Parameters.Add("@mdateStart", SqlDbType.NVarChar).Value = newMillin.mdateStart; comm.Parameters.Add("@mhurStart", SqlDbType.NVarChar).Value = newMillin.mhurStart; comm.Parameters.Add("@mdateEnd", SqlDbType.NVarChar).Value = newMillin.mdateEnd; comm.Parameters.Add("@mhurEnd", SqlDbType.NVarChar).Value = newMillin.mhurEnd; comm.Parameters.Add("@mdateAll", SqlDbType.NVarChar).Value = newMillin.mdateAll; comm.Parameters.Add("@mhurAll", SqlDbType.NVarChar).Value = newMillin.mhurAll; comm.Parameters.Add("@mless", SqlDbType.NVarChar).Value = newMillin.mless; comm.Parameters.Add("@mtime", SqlDbType.NVarChar).Value = newMillin.mtime; 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 (txtmdateAll.Text.Trim() == "") { MessageBox.Show("กรุณาเลือกวันก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtmdateAll.Focus(); return; } Millin newMillin = new Millin(); newMillin.psnCode = txtpsnCode.Text.Trim(); newMillin.cause= cbocause.Text.ToString(); newMillin.mFormat = cboformat.Text.ToString(); newMillin.note = txtNote.Text.Trim(); int _year = Convert.ToInt32(dtpmdateStart.Value.Year.ToString("0000")); string _dStart = dtpmdateStart.Value.Day.ToString("00") + "/" + dtpmdateStart.Value.Month.ToString("00") + "/" + (_year + 543); newMillin.mdateStart = _dStart; newMillin.mhurStart = txtmhurStart.Text.Trim(); int year = Convert.ToInt32(dtpmdateEnd.Value.Year.ToString("0000")); string dEnd = dtpmdateEnd.Value.Day.ToString("00") + "/" + dtpmdateEnd.Value.Month.ToString("00") + "/" + (_year + 543); newMillin.mdateEnd = dEnd; newMillin.mhurEnd = txtmhurEnd.Text.Trim(); newMillin.mdateAll = Convert.ToInt32(txtmdateAll.Text.Trim()); newMillin.mhurAll = Convert.ToInt32(txtmhurAll.Text.Trim()); if (rdbmless1.Checked == true) { newMillin.mless = 1; } else if (rdbmless2.Checked == true) { newMillin.mless = 0; } newMillin.mtime = Convert.ToInt32(txtmtime.Text.Trim()); int result = millinService.CreateMillin(newMillin); if (result > -1) { Console.WriteLine("Insert Complete"); lblresult.Visible = true; lblresult.Text = " บันทึกเรียบร้อย "; } else { Console.WriteLine("Insert Not Complete"); lblresult.Visible = true; lblresult.Text = " ไม่สามารถบันทึกข้อมูลได้"; } }