Guest User

Untitled

a guest
Mar 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!this.IsPostBack)
  4. {
  5. //Populating a DataTable from database.
  6. DataTable dt = this.GetData();
  7.  
  8. //Building an HTML string.
  9. StringBuilder html = new StringBuilder();
  10.  
  11. //Table start.
  12. html.Append("<table border = '1'>");
  13.  
  14. //Building the Header row.
  15. html.Append("<tr>");
  16. foreach (DataColumn column in dt.Columns)
  17. {
  18. html.Append("<th>");
  19. html.Append(column.ColumnName);
  20. html.Append("</th>");
  21. }
  22. html.Append("</tr>");
  23.  
  24. //Building the Data rows.
  25. foreach (DataRow row in dt.Rows)
  26. {
  27. html.Append("<tr>");
  28. foreach (DataColumn column in dt.Columns)
  29. {
  30. html.Append("<td>");
  31. html.Append(row[column.ColumnName]);
  32. html.Append("</td>");
  33. }
  34. html.Append("</tr>");
  35. }
  36.  
  37. //Table end.
  38. html.Append("</table>");
  39.  
  40. //Append the HTML string to Placeholder.
  41. PlaceHolder1.Controls.Add(new Literal { Text = html.ToString() });
  42. }
  43. }
  44.  
  45. private DataTable GetData()
  46. {
  47.  
  48. string webAppConnString = ConfigurationManager.ConnectionStrings["webAppConnString"].ConnectionString;
  49. using (SqlConnection con = new SqlConnection(webAppConnString))
  50. {
  51. con.Open();
  52. using (SqlCommand cmd = new SqlCommand("SELECT project_ID, user, date_time, projectAction, Comments FROM history;"))
  53. {
  54. using (SqlDataAdapter sda = new SqlDataAdapter())
  55. {
  56. cmd.Connection = con;
  57. sda.SelectCommand = cmd;
  58. using (DataTable dt = new DataTable())
  59. {
  60. sda.Fill(dt);
  61. return dt;
  62. }
  63. }
  64. }
  65.  
  66. }
  67. }
Add Comment
Please, Sign In to add comment