Oladotun Obileye

Oladotun Obileye

  • NA
  • 98
  • 17.9k

How to retreive data from database to a table in asp webform

May 7 2019 1:02 AM
Good day i created a table to be displayed on A adminlte template but it is not displaying the data from the database on the table
  1. namespace WebApplication4  
  2. {  
  3.     public partial class transahis : System.Web.UI.Page  
  4.     {  
  5.         SqlDataAdapter da;  
  6.         DataSet ds = new DataSet();  
  7.         StringBuilder htmlTable = new StringBuilder();  
  8.         protected void Page_Load(object sender, EventArgs e)  
  9.         {  
  10.             if (!Page.IsPostBack)  
  11.             {  
  12.                 binddata();  
  13.             }  
  14.         }  
  15.         private void binddata()  
  16.         {  
  17.             SqlConnection con = new SqlConnection();  
  18.             ;  
  19.             con.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\odt\Documents\login.mdf;Integrated Security=True;Connect Timeout=30";  
  20.             //con.Open();  
  21.             // SqlCommand cmd = new SqlCommand();  
  22.             //cmd.CommandText = "select * from [log] ";  
  23.             SqlCommand cmd = new SqlCommand("SELECT * FROM log ", con);  
  24.             da = new SqlDataAdapter(cmd);  
  25.             da.Fill(ds);  
  26.             con.Open();  
  27.             cmd.ExecuteNonQuery();  
  28.             con.Close();  
  29.             //cmd.Connection = con;  
  30.             // SqlDataReader dt = cmd.ExecuteReader();  
  31.             htmlTable.Append("<table border='1'>");  
  32.             htmlTable.Append("<tr style='background-color:green; color: White;'><th>ID</th><th>Name</th><th>password</th></tr>");  
  33.             if (!object.Equals(ds.Tables[0], null))  
  34.             {  
  35.                 if (ds.Tables[0].Rows.Count > 0)  
  36.                 {  
  37.                     for (int i = 0; i < ds.Tables[0].Rows.Count; i++)  
  38.                     {  
  39.                         htmlTable.Append("<tr style='color: White;'>");  
  40.                         htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Id"] + "</td>");  
  41.                         htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["username"] + "</td>");  
  42.                         htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["password"] + "</td>");  
  43.                         /* htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["ContactNo"] + "</td>")*/  
  44.                         ;  
  45.                         htmlTable.Append("</tr>");  
  46.                     }  
  47.                     htmlTable.Append("</table>");  
  48.                     hph.Controls.Add(new Literal { Text = htmlTable.ToString() });  
  49.                 }  
  50.                 else  
  51.                 {  
  52.                     htmlTable.Append("<tr>");  
  53.                     htmlTable.Append("<td align='center' colspan='4'>There is no Record.</td>");  
  54.                     htmlTable.Append("</tr>");  
  55.                 }  
  56.             }  
  57.         }  
  58.     }  
  59. }  

Answers (1)