private void BindListView() { string constr = ConfigurationManager.ConnectionStrings["RDB"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "SELECT UserName, Comments FROM Comments_table where itemName=@itemname"; cmd.Parameters.AddWithValue("@itemname", "item1"); cmd.Connection = con; using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); sda.Fill(dt); CommentsListView.DataSource = dt; CommentsListView.DataBind(); } } } }
private void Load_Stuff() { if (string.IsNullOrEmpty(Request.Params["id"])) { Response.Redirect("~/Index.aspx"); return; } ArticleImage.Attributes.Add("style", "float: left; margin-right: 10px;"); string id = Request.Params["id"].ToString(); string query = "SELECT articles.Id as id, Articles.Title as title, Articles.Content as content, Articles.Date_Created as date, Articles.Category as category, Articles.ImageLink as image, Users.First_Name as fname, Users.Last_Name as lname FROM " + "Articles JOIN Users ON Articles.Author = Users.Id " + "WHERE Articles.Id = @id;"; SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyDb"].ConnectionString); con.Open(); try { SqlCommand com = new SqlCommand(query, con); com.Parameters.AddWithValue("id", id.ToString()); SqlDataReader reader = com.ExecuteReader(); reader.Read(); if (new UrlCheck().checkUrl(reader["content"].ToString())) { ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:goToPage('" + reader["content"].ToString() + "');", true); Response.Redirect("~/Index.aspx"); } ArticleTitle.Text = reader["title"].ToString(); ArticleBody.Text = reader["content"].ToString(); ArticleAuthor.Text = reader["fname"].ToString() + " " + reader["lname"]; ArticleDate.Text = reader["date"].ToString(); ArticleImage.ImageUrl = reader["image"].ToString(); string commentsId = reader["id"].ToString(); string queryComments = "SELECT Comments.Comment as comment, Comments.Date_Created as date, Users.First_Name + ' ' + Users.Last_Name as username, Users.Email as email " + "FROM Comments JOIN Users ON Comments.UserId = Users.Id WHERE Comments.Article = @id ORDER BY CONVERT(DateTime, Comments.Date_Created, 101) DESC;"; SqlConnection conComments = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyDb"].ConnectionString); conComments.Open(); try { SqlCommand comComments = new SqlCommand(queryComments, conComments); comComments.Parameters.AddWithValue("id", commentsId); SqlDataReader readerComments = comComments.ExecuteReader(); CommentsListView.DataSource = readerComments; CommentsListView.DataBind(); } catch (Exception ex) { IndexErrorLabel.Text = "Failed to load comments. Try again later." + ex.Message; } finally { conComments.Close(); } } catch (Exception ex) { IndexErrorLabel.Text = "Failed to load article. Try again later. "; } finally { con.Close(); } }