public static int Insert(DailySheetAddRequest model) { //Insert returns new record Id, so must declare int Id for return statement int id = 0; //Get connection to DB and tell it to run proc DataProvider.ExecuteNonQuery(GetConnection, "dbo.Dailylogs_Insert" //inputParamMapper creates new Collection to get values from DB , inputParamMapper : delegate(SqlParameterCollection paramCollection) { //Put values from request model into paramCollection to deliver to DB SetCommonParameters(model, paramCollection); //Make space in paramCollection for Id value, indicate direction is output, not input SqlParameter p = new SqlParameter("@Id", System.Data.SqlDbType.Int); p.Direction = System.Data.ParameterDirection.Output; paramCollection.Add(p); //grab value of @Id, parse it from string and store int in id }, returnParameters : delegate(SqlParameterCollection param) { int.TryParse(param["@Id"].Value.ToString(), out id); } ); //return the id of the new record return(id); }
private static void SetCommonParameters(DailySheetAddRequest model, SqlParameterCollection paramCollection) { paramCollection.AddWithValue("@RoomNumber", model.RoomNumber); paramCollection.AddWithValue("@CardNumber", model.CardNumber); paramCollection.AddWithValue("@FirstName", model.FirstName); paramCollection.AddWithValue("@LastName", model.LastName); paramCollection.AddWithValue("@Checkin", model.Checkin); paramCollection.AddWithValue("@Checkout", model.Checkout); paramCollection.AddWithValue("@Transit", model.Transit); paramCollection.AddWithValue("@Tax", model.Tax); paramCollection.AddWithValue("@Exempt", model.Exempt); paramCollection.AddWithValue("@Total", model.Total); }
public HttpResponseMessage Insert(DailySheetAddRequest model) { if (!ModelState.IsValid && model != null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } try { ItemResponse <int> response = new ItemResponse <int>(); int id = DailySheetsService.Insert(model); response.Item = id; return(Request.CreateResponse(HttpStatusCode.OK, response)); } catch (Exception ex) { ErrorResponse response = new ErrorResponse(ex.Message); return(Request.CreateResponse(HttpStatusCode.InternalServerError, response)); } }