public int UpdateArticle(Article aArticle) { SqlConnection connection = new SqlConnection(connectionString); string query = "UPDATE ArticleTable SET Title='" + aArticle.Title + "',Content='" + aArticle.Content + "',DateTime='" + aArticle.DateTime + "',UserId='" + aArticle.UserId + "',CategoryId='" + aArticle.CategoryId + "',Status='" + aArticle.Status + "',HitCounter='" + aArticle.HitCounter + "' WHERE Id='" + aArticle.Id + "'"; SqlCommand sqlCommand = new SqlCommand(query, connection); connection.Open(); int rowaffected = sqlCommand.ExecuteNonQuery(); connection.Close(); return rowaffected; }
public int CreateArticle(Article aArticle) { SqlConnection connection = new SqlConnection(connectionString); string query = "INSERT INTO ArticleTable(Title,Content,DateTime,UserId,CategoryId,Status,HitCounter) VALUES('" + aArticle.Title + "','" + aArticle.Content + "','" + aArticle.DateTime + "','" + aArticle.UserId + "','" + aArticle.CategoryId + "','" + aArticle.Status + "','" + aArticle.HitCounter + "')"; SqlCommand sqlCommand = new SqlCommand(query, connection); connection.Open(); int rowaffected= sqlCommand.ExecuteNonQuery(); connection.Close(); return rowaffected; }
protected void saveButton_Click(object sender, EventArgs e) { User user = (User) Session["user"]; Article aArticle = new Article(); aArticle.Content = Request.Form["edit"]; //show.InnerHtml = aArticle.Content; aArticle.Title = titleTextBox.Text; aArticle.DateTime=DateTime.Now; aArticle.UserId = user.Id; aArticle.CategoryId = Convert.ToInt16(DropDownList1.SelectedValue); aArticle.Status = false; aArticle.HitCounter = 0; if (aManagerArticle.CreateArticle(aArticle) > 0) { Response.Redirect("Home.aspx"); } }
protected void saveButton_Click(object sender, EventArgs e) { User user = (User)Session["user"]; Article aArticle = null; if (saveButton.Text.Equals("Create")) { aArticle = new Article(); } else if (saveButton.Text.Equals("Update")) { aArticle = aManagerArticle.GetArticleById(int.Parse(Request["articleId"])); } aArticle.Title = titleTextBox.Text; aArticle.Content = Request.Form["ctl00$body$edit"]; aArticle.CategoryId = Convert.ToInt16(categoryDropDownList.SelectedValue); aArticle.DateTime = DateTime.Now; if (saveButton.Text.Equals("Create")) { aArticle.UserId = user.Id; aArticle.Status = false; aArticle.HitCounter = 0; if (aManagerArticle.CreateArticle(aArticle) > 0) { Response.Redirect("WriteArticle.aspx"); } } else if (saveButton.Text.Equals("Update")) { if (aManagerArticle.UpdateArticle(aArticle) > 0) { Response.Redirect("WriteArticle.aspx"); } } }
public List<Article> GetArticleListSortedByHitCounter() { SqlConnection connection = new SqlConnection(connectionString); string query = "SELECT * FROM ArticleTable WHERE Status='True' ORDER BY HitCounter DESC"; SqlCommand sqlCommand = new SqlCommand(query, connection); connection.Open(); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); List<Article> articleList = new List<Article>(); while (sqlDataReader.Read()) { Article article = new Article(); article.Id = int.Parse(sqlDataReader["Id"].ToString()); article.Title = sqlDataReader["Title"].ToString(); article.Content = WebUtility.HtmlDecode(sqlDataReader["Content"].ToString()); article.DateTime = (DateTime)sqlDataReader["Datetime"]; article.UserId = int.Parse(sqlDataReader["UserId"].ToString()); article.CategoryId = int.Parse(sqlDataReader["CategoryId"].ToString()); article.Status = (bool)sqlDataReader["Status"]; article.HitCounter = int.Parse(sqlDataReader["HitCounter"].ToString()); articleList.Add(article); } connection.Close(); return articleList; }
public int UpdateArticle(Article aArticle) { return aGatewayArticle.UpdateArticle(aArticle); }
public int CreateArticle(Article aArticle) { return aGatewayArticle.CreateArticle(aArticle); }
internal Article GetArticleById(int id) { SqlConnection connection = new SqlConnection(connectionString); string query = "SELECT * FROM ArticleTable WHERE Id='" + id + "'"; SqlCommand command = new SqlCommand(query, connection); connection.Open(); List<Article> articleList = new List<Article>(); using (SqlDataReader sqlDataReader = command.ExecuteReader()) { while (sqlDataReader.Read()) { Article article = new Article(); article.Id = int.Parse(sqlDataReader["Id"].ToString()); article.Title = sqlDataReader["Title"].ToString(); article.Content = WebUtility.HtmlDecode(sqlDataReader["Content"].ToString()); article.DateTime = (DateTime)sqlDataReader["Datetime"]; article.UserId = int.Parse(sqlDataReader["UserId"].ToString()); article.CategoryId = int.Parse(sqlDataReader["CategoryId"].ToString()); article.Status = (bool)sqlDataReader["Status"]; article.HitCounter = int.Parse(sqlDataReader["HitCounter"].ToString()); articleList.Add(article); } connection.Close(); } if (articleList.Count == 0) { return null; } else { return articleList[0]; } }