1.HTML page
<body><form id="Form1" runat="server"><div id="divusers" runat="server"></div> </form></body>
2. Add a Namespace
using System.Configuration; using System.Data.SqlClient; using System.Data; using System.Text;
3. Load the form in the Page_Load event
protected voidPage_Load (Objectsender, EventArgs e) { if(!IsPostBack) { stringconstring = configurationmanager.connectionstrings["demosdatabaseconnectionstring"]. ConnectionString; stringquery ="SELECT * from Customers"; using(SqlConnection con =NewSqlConnection (constring)) { using(SqlCommand cmd =NewSqlCommand (query)) { using(SqlDataAdapter SDA =NewSqlDataAdapter ()) {cmd. Connection=con; Sda. SelectCommand=cmd; using(DataTable dt =NewDataTable ()) {SDA. Fill (DT); StringBuilder Str=NewStringBuilder (); StringBuilder str1=NewStringBuilder (); Str. Append ("<table>"); Str. Append ("<tr>"); for(intK =0; K < dt. Columns.count; k++) {str. Append ("<th>"); Str. Append (dt. COLUMNS[K]. ColumnName); Str. Append ("</th>"); } str. Append ("<th>"); Str. Append (""); Str. Append ("</th>"); Str. Append ("</tr>"); for(inti =0; i < dt. Rows.Count; i++) {str. Append ("<tr>"); for(intK =0; K < dt. Columns.count; k++) {str. Append ("<td>"); Str. Append (dt. Rows[i][k]); Str. Append ("</td>"); } str. Append ("<td>"); Str. Append ("<input type= ' text ' id= ' txtID"+ (i +1) +"' runat= ' server '/>"); Str. Append ("</td>"); Str. Append ("</tr>"); } str. Append ("</table>"); Str. Append ("<table>"); Str. Append ("<tr>"); for(intK =0; K < dt. Columns.count; k++) {str. Append ("<th>"); Str. Append ("Head"); Str. Append ("</th>"); } str. Append ("</tr>"); for(inti =0; i < dt. Rows.Count; i++) {str. Append ("<tr>"); for(intK =0; K < dt. Columns.count; k++) {str. Append ("<td>"); Str. Append ("Test"); Str. Append ("</td>"); } str. Append ("</tr>"); } str. Append ("</table>"); Divusers.innerhtml=Str. ToString (); } } } } } }
View Code
The data that will be fetched from the database is displayed as an HTML table