The data that will be fetched from the database, in the form of an HTML table

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.