DataSet binds data to the HTML Table

Source: Internet
Author: User
Execute the Default2.aspx Page code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
    <title></title>
<body>
    <form id="form1" runat="server">
    <div id="display" runat="server">Table Will Go Here</div>
 
    </form>
</body>

Default. aspx. cs background code
   1:  using System;
   2:  using System.Data;
   3:  using System.Data.SqlClient;
   4:  using System.Web.Configuration;
   5:   
   6:  public partial class Default2 : System.Web.UI.Page
   7:  {
   8:      protected void Page_Load(object sender, EventArgs e)
   9:      {
  10:          // Connection set up
  11:          String strConnection = WebConfigurationManager.ConnectionStrings["NorthWind"].ConnectionString;
  12:   
  13:          SqlConnection objConnection = new SqlConnection(strConnection);
  14:          String strSQL = "SELECT ProductName, UnitsInStock FROM Products";
  15:          // DataAdapter setup
  16:          SqlDataAdapter objAdapter = new SqlDataAdapter(strSQL, objConnection);
  17:          // DataSet & Adapter & Table
  18:          DataSet objDataSet = new DataSet();
  19:          objAdapter.Fill(objDataSet, "dtProducts");
  20:          String strResultsHolder;
  21:          strResultsHolder = "<table width=80% border=1>";
  22:          strResultsHolder += "<tr>";
  23:          foreach (DataColumn c in objDataSet.Tables["dtProducts"].Columns)
  24:          {
  25:              strResultsHolder += "<td>" + c.ColumnName + "</td>";
  26:          }
  27:          strResultsHolder += "</tr>";
  28:          int value, blankValue;
  29:          foreach (DataRow r in objDataSet.Tables["dtProducts"].Rows)
  30:          {
  31:              value = 100 * Convert.ToInt32(r["UnitsInStock"]) / 125;
  32:              blankValue = 100 - value;
  33:              strResultsHolder += "<tr><td width=30%>" + r["ProductName"] + "</td>" +
  34:                  "<td width=60%><table width=100%><tr>" + "<td width=" + value.ToString() +
  35:              "% bgcolor=#9933FF>" + "&nbsp;</td>" + "<td width=" + blankValue.ToString() +
  36:              "%>&nbsp;</td>" + "</tr></table></td>" + "<td width=10%>" + r["UnitsInStock"].ToString() + "</td></tr>";
  37:          }
  38:          strResultsHolder += "</table>";
  39:          display.InnerHtml = strResultsHolder;
  40:      }
  41:  }

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.