public ActionResult Create(AgencyTypesModel AgencyTypesObj) { //------------------------------------------ //Check ModelState //------------------------------------------ if (!ModelState.IsValid) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Invalid data")); } //------------------------------------------ try { bool result = AgencyTypesFactor.Create(AgencyTypesObj); if (result == true) { return(List(1, 25, null, null, null, null)); } else { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Saving operation faild")); } } catch (Exception ex) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.Message)); } }
//------------------------------------------------------------------------------------------------------ #endregion #region /*--------- Create ---------*\ //------------------------------------------------------------------------------------------------------ /// <summary> /// Converts the object properties to SQL paramters and executes the create procedure /// and updates the object with the SQL data by reference. /// </summary> /// <param name="AgencyTypesObj">Model object.</param> /// <returns>The result of create query.</returns> //-------------------------------------------------------------------- public bool Create(AgencyTypesModel AgencyTypesObj) { bool result = false; using (SqlConnection myConnection = GetSqlConnection()) { SqlCommand myCommand = new SqlCommand("[dbo].[AgencyTypes_Create]", myConnection); myCommand.CommandType = CommandType.StoredProcedure; //---------------------------------------------------------------------------------------------- // Set the parameters //---------------------------------------------------------------------------------------------- myCommand.Parameters.Add("@AgencyTypeID", SqlDbType.Int, 4).Direction = ParameterDirection.Output; myCommand.Parameters.Add("@Title", SqlDbType.NVarChar, 128).Value = AgencyTypesObj.Title; //---------------------------------------------------------------------------------------------- // Execute the command //---------------------------------------------------------------------------------------------- myConnection.Open(); if (myCommand.ExecuteNonQuery() > 0) { result = true; //Get ID value from database and set it in object AgencyTypesObj.AgencyTypeID = (int)myCommand.Parameters["@AgencyTypeID"].Value; } myConnection.Close(); return result; //---------------------------------------------------------------------------------------------- } }
//------------------------------------------------------------------------------------------------------ #endregion #region /*--------- Update ---------*\ //------------------------------------------------------------------------------------------------------ /// <summary> /// Converts the object properties to SQL paramters and executes the update procedure. /// </summary> /// <param name="AgencyTypesObj">Model object.</param> /// <returns>The result of update query.</returns> //-------------------------------------------------------------------- public bool Update(AgencyTypesModel AgencyTypesObj) { bool result = false; using (SqlConnection myConnection = GetSqlConnection()) { SqlCommand myCommand = new SqlCommand("[dbo].[AgencyTypes_Update]", myConnection); myCommand.CommandType = CommandType.StoredProcedure; //---------------------------------------------------------------------------------------------- // Set the parameters //---------------------------------------------------------------------------------------------- myCommand.Parameters.Add("@AgencyTypeID", SqlDbType.Int, 4).Value = AgencyTypesObj.AgencyTypeID; myCommand.Parameters.Add("@Title", SqlDbType.NVarChar, 128).Value = AgencyTypesObj.Title; //---------------------------------------------------------------------------------------------- // Execute the command //---------------------------------------------------------------------------------------------- myConnection.Open(); if (myCommand.ExecuteNonQuery() > 0) { result = true; } myConnection.Close(); return result; //---------------------------------------------------------------------------------------------- } }
//------------------------------------------------------------------------------------------------------ #endregion #region --------------GetObject-------------- //------------------------------------------------------------------------------------------------------ /// <summary> /// Gets the spesific record. /// </summary> /// <param name="ID">The model id.</param> /// <returns>Model object.</returns> //-------------------------------------------------------------------- public static AgencyTypesModel GetObject(int AgencyTypeID) { AgencyTypesModel AgencyTypesObj = null; List <AgencyTypesModel> list = AgencyTypesSqlDataPrvider.Instance.Get(AgencyTypeID); if (list != null && list.Count > 0) { AgencyTypesObj = list[0]; } return(AgencyTypesObj); }
public ActionResult GetObject(int id) { try { AgencyTypesModel AgencyTypesObj = AgencyTypesFactor.GetObject(id); if (AgencyTypesObj == null) { AgencyTypesObj = new AgencyTypesModel(); } return(Json(AgencyTypesObj, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.Message)); } }
//------------------------------------------------------------------------------------------------------ #endregion #region /*--------- PopulateModelFromIDataReader ---------*\ //------------------------------------------------------------------------------------------------------ /// <summary> /// Populates model from IDataReader . /// </summary> /// <param name="reader"></param> /// <returns>Model object.</returns> //-------------------------------------------------------------------- private AgencyTypesModel PopulateModelFromIDataReader(IDataReader reader) { //Create a new object AgencyTypesModel AgencyTypesObj = new AgencyTypesModel(); //Fill the object with data //------------------------------------------------ //[AgencyTypeID] //------------------------------------------------ if (reader["AgencyTypeID"] != DBNull.Value) AgencyTypesObj.AgencyTypeID = (int)reader["AgencyTypeID"]; //------------------------------------------------ //------------------------------------------------ //[Title] //------------------------------------------------ if (reader["Title"] != DBNull.Value) AgencyTypesObj.Title = (string)reader["Title"]; //------------------------------------------------ //Return the populated object return AgencyTypesObj; }
//------------------------------------------------------------------------------------------------------ #endregion #region --------------Update-------------- //------------------------------------------------------------------------------------------------------ /// <summary> /// Updates model object by calling sql data provider update method. /// </summary> /// <param name="AgencyTypesObj">The model object.</param> /// <returns>The result of update operation.</returns> //-------------------------------------------------------------------- public static bool Update(AgencyTypesModel AgencyTypesObj) { return(AgencyTypesSqlDataPrvider.Instance.Update(AgencyTypesObj)); }