public void InsertWithReminder(EmployeeEntity employeeEntity, ReminderEntity reminderEntity) { using (SqlConnection connection = DALHelper.CreateSqlDbConnection()) { // Start a local transaction. SqlTransaction sqlTran = connection.BeginTransaction(); // Enlist a command in the current transaction. try { SqlCommand command = connection.CreateCommand(); command.Transaction = sqlTran; new EmployeeMapper().Insert(employeeEntity, command); command = connection.CreateCommand(); command.Transaction = sqlTran; reminderEntity.EntityPK = employeeEntity.Id.ToString(); new ReminderMapper().Insert(reminderEntity, command); // Commit the transaction. sqlTran.Commit(); } catch (Exception) { // Handle the exception if the transaction fails to commit. try { // Attempt to roll back the transaction. sqlTran.Rollback(); } catch (Exception) { // Throws an InvalidOperationException if the connection // is closed or the transaction has already been rolled // back on the server. } } } }
public void Insert(ReminderEntity t, SqlCommand transactionCMD) { if (transactionCMD != null) { transactionCMD.CommandText = "usp_InsertReminder"; transactionCMD.CommandType = System.Data.CommandType.StoredProcedure; transactionCMD.Parameters.AddWithValue("@Url", t.Url); transactionCMD.Parameters.AddWithValue("@EntityPK", t.EntityPK); transactionCMD.Parameters.AddWithValue("@EntityPKType", t.EntityPKType); transactionCMD.Parameters.AddWithValue("@Type", t.ReminderType); transactionCMD.ExecuteNonQuery(); } else { SqlConnection conn = null; SqlCommand cmd = null; try { conn = DALHelper.CreateSqlDbConnection(); cmd = new SqlCommand("usp_InsertReminder", conn); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Url", t.Url); cmd.Parameters.AddWithValue("@EntityPK", t.EntityPK); cmd.Parameters.AddWithValue("@EntityPKType", t.EntityPKType); cmd.Parameters.AddWithValue("@Type", t.ReminderType); t.Id = Convert.ToInt32(cmd.ExecuteScalar()); } catch (Exception) { throw; } finally { conn.Close(); cmd.Dispose(); conn.Dispose(); } } }
protected void ProceedButton_Click(object sender, EventArgs e) { EmployeeEntity entity = new EmployeeEntity(); entity.EmployeeNo = EmployeeNoTextBox.Text; entity.Firstname = FirstnameTextBox.Text; entity.Middlename = ""; entity.Middlename = MiddlenameTextBox.Text; entity.Lastname = LastnameTextBox.Text; DateTime dt; if (DateTime.TryParseExact(DateOfBirthTextBox.Text, ConfigurationManager.AppSettings["DateFormat"], null, System.Globalization.DateTimeStyles.None, out dt)) { entity.DateOfBirth = dt; } entity.Gender = (GenderEnum)Convert.ToInt32(GenderDropDownList.SelectedValue); entity.NationalityId = Convert.ToInt32(NationalityDropDownList.SelectedValue); entity.CountryId = Convert.ToInt32(CountryDropDownList.SelectedValue); entity.Address = AddressTextBox.Text; entity.PersonalNumber = PersonalNumberTextBox.Text; entity.WorkEmail = WorkEmailTextBox.Text; entity.MobilePhone = MobilePhoneTextBox.Text; entity.OtherEmail = OtherEmailTextBox.Text; entity.City = CityTextBox.Text; entity.BankId = Convert.ToInt32(BankDropDownList.SelectedValue); entity.AccountNumber = AccountNumberTextBox.Text; entity.MaritalStatus = (MaritalStatusEnum)Convert.ToInt32(MaritalStatusDropDownList.SelectedValue); if (Session["fileContents_"] != null) { byte[] fileContents = (byte[])Session["fileContents_"]; entity.Image = fileContents; } EmployeeMapper mapper = new EmployeeMapper(); try { if (Request.QueryString["action"] == "update" && Request.QueryString["EmployeeId"] != null) { entity.Id = Convert.ToInt32(Request.QueryString["EmployeeId"]); mapper.Update(entity); Response.Redirect("EducationAndExperience.aspx?action=update&EmployeeId=" + entity.Id); } else { ReminderEntity reminder = new ReminderEntity(); reminder.EntityPK = entity.Id.ToString(); reminder.EntityPKType = typeof(int).ToString(); reminder.ReminderType = ReminderEnum.EmployeeNoContract; reminder.Url = "/HRM/HR-Managment/Employee/EmployeesWithoutContract.aspx"; new EmployeeMapperTransaction().InsertWithReminder(entity, reminder); Response.Redirect("EducationAndExperience.aspx?EmployeeId=" + entity.Id); } } catch (SqlException ex) { StringBuilder sb = new StringBuilder(); sb.Append("<script language='javascript'>displayNoty('The Employee No { " + EmployeeNoTextBox.Text + " } has been used, plase write another number');</script>"); // if the script is not already registered if (!Page.ClientScript.IsClientScriptBlockRegistered(Page.GetType(), "HeyPopup")) ClientScript.RegisterClientScriptBlock(Page.GetType(), "HeyPopup", sb.ToString()); } finally { } }