public void BlogPostFormView_InsertItem(BlogPost blogPost) { if (ModelState.IsValid) { try { blogPost.UserId = Convert.ToInt32(Request.Form["selectList"]); Service.InsertBlogPost(blogPost); Page.SetTempData("SuccessMessage", "Meddelandet lades till!"); Response.RedirectToRoute("Posts"); Context.ApplicationInstance.CompleteRequest(); } catch (AggregateException ex) { foreach (var vr in ex.InnerExceptions) { ModelState.AddModelError(String.Empty, vr.Message); } } catch (Exception) { ModelState.AddModelError(String.Empty, "CODEBEHIND Ett fel inträffade när posten skulle läggas till."); } } }
public void InsertBlogPost(BlogPost blogPost) { ICollection<ValidationResult> validationResults; if (!blogPost.Validate(out validationResults)) { throw new AggregateException("Objektet klarade inte valideringen.", validationResults.Select(vr => new ValidationException(vr.ErrorMessage)).ToList().AsReadOnly()); } BlogPostDAL.InsertBlogPost(blogPost); }
public void UpdateBlogPost(BlogPost blogPost) { using (var conn = CreateConnection()) { try { SqlCommand cmd = new SqlCommand("appSchema.uspUpdateBlogPost", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@BlogPostId", SqlDbType.Int, 4).Value = blogPost.BlogPostId; cmd.Parameters.Add("@Title", SqlDbType.VarChar, 50).Value = blogPost.Title; cmd.Parameters.Add("@Message", SqlDbType.VarChar, 200).Value = blogPost.Message; conn.Open(); cmd.ExecuteNonQuery(); } catch { throw new ApplicationException("Ett fel inträffade när posterna skulle hämtas."); } } }