private static bool ValidateEmail(EmailAddress emailToValidate, ref BrokenRuleCollection brokenRules) { //Set default return value to true bool returnValue = true; //Validate email address if (emailToValidate != null) { if (!string.IsNullOrEmpty(emailToValidate.EmailAddressString)) { if (!EmailValidator.IsValid(emailToValidate.EmailAddressString.Trim())) { //Email was not in a valid format brokenRules.Add("Email Address", emailToValidate.EmailAddressString.Trim() + "is an invalid email format."); returnValue = false; } } else { //Emailis required brokenRules.Add("Email Address", "Email is required"); returnValue = false; } } else { //Generic error - class not instantiated brokenRules.Add("Email Address", "Email class was not instantiated."); returnValue = false; } return returnValue; }
public static int Save(int employeeId, EmailAddress emailToSave) { //Instantiate a brokenRules collection and check for any validation errors BrokenRuleCollection saveBrokenRules = new BrokenRuleCollection(); if (employeeId <= 0) { saveBrokenRules.Add("Employee", "Invalid Id"); } //Make sure email is in correct format ValidateEmail(emailToSave, ref saveBrokenRules); //email Type is required if (emailToSave.EntityType.EntityTypeId <= 0) { saveBrokenRules.Add("Email Address Type", "Type is required."); } //Check for validation errors if (emailToSave.EntityType.EntityTypeId <= 0) { throw new BLLException("There was an error saving email.", saveBrokenRules); } else { //Validation a success - call the data access layer to save return EmailAddressDAL.Save(employeeId, emailToSave); } }
/// <summary Fill Data Record> /// Initializes a new instance of the Email Address class and fills it with the data from the IDataRecord /// </summary> /// <param name="myDataRecord"></param> /// <returns></returns> private static EmailAddress FillDataRecord(IDataRecord myDataRecord) { EmailAddress myEmailAddressObject = new EmailAddress(); myEmailAddressObject.EmailId = myDataRecord.GetInt32(myDataRecord.GetOrdinal("emailID")); if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("emailAddress"))) { myEmailAddressObject.EmailAddressString = myDataRecord.GetString(myDataRecord.GetOrdinal("emailAddress")); } if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("entityTypeId"))) { myEmailAddressObject.EntityType.EntityTypeId = myDataRecord.GetInt32(myDataRecord.GetOrdinal("entityTypeId")); if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("entityTypeName"))) { myEmailAddressObject.EntityType.EntityTypeName = myDataRecord.GetString(myDataRecord.GetOrdinal("entityTypeName")); } } return myEmailAddressObject; }
public static int Save(int employeeId, EmailAddress emailToSave) { ExecuteTypeEnum queryId = ExecuteTypeEnum.InsertItem; int result = 0; if (emailToSave.EmailId > 0) { queryId = ExecuteTypeEnum.UpdateItem; } using (SqlConnection myConnection = new SqlConnection(AppConfiguration.ConnectionString)) { using (SqlCommand myCommand = new SqlCommand("usp_ExecuteEmail", myConnection)) { myCommand.CommandType = CommandType.StoredProcedure; myCommand.Parameters.AddWithValue("@QueryId", queryId); myCommand.Parameters.AddWithValue("@EmployeeId", employeeId); if (emailToSave.EmailId != null) { myCommand.Parameters.AddWithValue("@EmailId", emailToSave.EmailId); } if (emailToSave.EmailAddressString != null) { myCommand.Parameters.AddWithValue("@EmailAddress", emailToSave.EmailAddressString); } if (emailToSave.EntityType.EntityTypeId != null) { myCommand.Parameters.AddWithValue("@EntityTypeId", emailToSave.EntityType.EntityTypeId); } //add return output parameter to command object myCommand.Parameters.Add(HelperDAL.GetReturnParameterInt("ReturnValue")); myConnection.Open(); myCommand.ExecuteNonQuery(); //Get return value from stored procedure and return Id result = (int)myCommand.Parameters["@ReturnValue"].Value; } myConnection.Close(); } return result; }