/// <summary> /// Saves this instance. /// </summary> /// <param name="entity">The custom field to save.</param> /// <returns></returns> public static bool SaveOrUpdate(CustomField entity) { if (entity == null) throw new ArgumentNullException("entity"); if (entity.ProjectId <= Globals.NEW_ID) throw (new ArgumentException("Cannot save custom field, the project id is invalid")); if (string.IsNullOrEmpty(entity.Name)) throw (new ArgumentException("The custom field name cannot be empty or null")); if (entity.Id > Globals.NEW_ID) if (DataProviderManager.Provider.UpdateCustomField(entity)) { UpdateCustomFieldView(entity.ProjectId); return true; } var tempId = DataProviderManager.Provider.CreateNewCustomField(entity); if (tempId <= 0) return false; entity.Id = tempId; UpdateCustomFieldView(entity.ProjectId); return true; }
/// <summary> /// Updates the custom field. /// </summary> /// <param name="customFieldToUpdate">The custom field to update.</param> /// <returns></returns> public override bool UpdateCustomField(CustomField customFieldToUpdate) { if (customFieldToUpdate == null) throw new ArgumentNullException("customFieldToUpdate"); using (var sqlCmd = new SqlCommand()) { AddParamToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null); AddParamToSqlCmd(sqlCmd, "@CustomFieldId", SqlDbType.Int, 0, ParameterDirection.Input, customFieldToUpdate.Id); AddParamToSqlCmd(sqlCmd, "@CustomFieldName", SqlDbType.NText, 50, ParameterDirection.Input, customFieldToUpdate.Name); AddParamToSqlCmd(sqlCmd, "@CustomFieldDataType", SqlDbType.Int, 0, ParameterDirection.Input, customFieldToUpdate.DataType); AddParamToSqlCmd(sqlCmd, "@ProjectId", SqlDbType.Int, 0, ParameterDirection.Input, customFieldToUpdate.ProjectId); AddParamToSqlCmd(sqlCmd, "@CustomFieldRequired", SqlDbType.Bit, 0, ParameterDirection.Input, customFieldToUpdate.Required); AddParamToSqlCmd(sqlCmd, "@CustomFieldTypeId", SqlDbType.Int, 0, ParameterDirection.Input, (int)customFieldToUpdate.FieldType); SetCommandType(sqlCmd, CommandType.StoredProcedure, SP_CUSTOMFIELD_UPDATE); ExecuteScalarCmd(sqlCmd); return ((int)sqlCmd.Parameters["@ReturnValue"].Value == 0); } }
/// <summary> /// Creates the new custom field. /// </summary> /// <param name="newCustomField">The new custom field.</param> /// <returns></returns> public override int CreateNewCustomField(CustomField newCustomField) { if (newCustomField == null) throw new ArgumentNullException("newCustomField"); using (var sqlCmd = new SqlCommand()) { AddParamToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null); AddParamToSqlCmd(sqlCmd, "@CustomFieldName", SqlDbType.NText, 50, ParameterDirection.Input, newCustomField.Name); AddParamToSqlCmd(sqlCmd, "@CustomFieldDataType", SqlDbType.Int, 0, ParameterDirection.Input, newCustomField.DataType); AddParamToSqlCmd(sqlCmd, "@ProjectId", SqlDbType.Int, 0, ParameterDirection.Input, newCustomField.ProjectId); AddParamToSqlCmd(sqlCmd, "@CustomFieldRequired", SqlDbType.Bit, 0, ParameterDirection.Input, newCustomField.Required); AddParamToSqlCmd(sqlCmd, "@CustomFieldTypeId", SqlDbType.Int, 0, ParameterDirection.Input, (int)newCustomField.FieldType); SetCommandType(sqlCmd, CommandType.StoredProcedure, SP_CUSTOMFIELD_CREATE); ExecuteScalarCmd(sqlCmd); return ((int)sqlCmd.Parameters["@ReturnValue"].Value); } }
public abstract bool UpdateCustomField(CustomField customFieldToUpdate);
public abstract int CreateNewCustomField(CustomField newCustomField);
/// <summary> /// Handles the Click event of the lnkAddCustomField control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param> protected void lnkAddCustomField_Click(object sender, EventArgs e) { var newName = txtName.Text.Trim(); if (newName == String.Empty) return; var dataType = (ValidationDataType)Enum.Parse(typeof(ValidationDataType), dropDataType.SelectedValue); var fieldType = (CustomFieldType)Enum.Parse(typeof(CustomFieldType), rblCustomFieldType.SelectedValue); var required = chkRequired.Checked; var newCustomField = new CustomField { ProjectId = ProjectId, Name = newName, DataType = dataType, Required = required, FieldType = fieldType }; if (CustomFieldManager.SaveOrUpdate(newCustomField)) { txtName.Text = ""; dropDataType.SelectedIndex = 0; chkRequired.Checked = false; BindCustomFields(); } else { lblError.Text = LoggingManager.GetErrorMessageResource("SaveCustomFieldError"); } }