protected void RemoveExternArticleButton_Click(object sender, EventArgs e) { using (var _db = new WorldOfNews.Models.ArticleContext()) { int articleId = Convert.ToInt16(DropDownRemoveExternArticle.SelectedValue); var myItem = (from c in _db.ExternArticles where c.ArticleID == articleId select c).FirstOrDefault(); if (myItem != null) { _db.ExternArticles.Remove(myItem); _db.SaveChanges(); // Reload the page. string pageUrl = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.Count() - Request.Url.Query.Count()); Response.Redirect(pageUrl + "?ExternArticleAction=remove"); } else { LabelRemoveStatus.Text = "Unable to locate article."; } } }
protected void RemoveCategoryButton_Click(object sender, EventArgs e) { using (var _db = new WorldOfNews.Models.ArticleContext()) { int categoryId; try { categoryId = Convert.ToInt16(DropDownRemoveCategory.SelectedValue); } catch (Exception ex) { categoryId = -1; } var myItems = from c in _db.Articles where c.CategoryID == categoryId select c; if (myItems != null) { foreach (var iter in myItems) { _db.Articles.Remove(iter); } var myCategory = (from c in _db.Categories where c.CategoryID == categoryId select c).FirstOrDefault(); if (myCategory != null) { _db.Categories.Remove(myCategory); _db.SaveChanges(); } else { LabelRemoveStatus.Text += "Category not found."; } string pageUrl = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.Count() - Request.Url.Query.Count()); Response.Redirect(pageUrl + "?CategoryAction=remove"); } else { LabelRemoveStatus.Text += "No category found."; } } }