ADO Net Database ASP.Net Tutorial

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"  Inherits="DataReader" %>



    Untitled Page


    
    

      

Employees


    
    

    


File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;
using System.Web.Configuration;
public partial class DataReader : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    string connectionString = WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
    SqlConnection con = new SqlConnection(connectionString);
    string sql = "SELECT * FROM Employees";
    SqlCommand cmd = new SqlCommand(sql, con);
    con.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    StringBuilder htmlStr = new StringBuilder("");
    while (reader.Read())
    {
      htmlStr.Append("
  • ");
          htmlStr.Append(reader["TitleOfCourtesy"]);
          htmlStr.Append(" ");
          htmlStr.Append(reader.GetString(1));
          htmlStr.Append("
    , ");
          htmlStr.Append(reader.GetString(2));
          htmlStr.Append(" - employee from ");
          htmlStr.Append(reader.GetDateTime(6).ToString("d"));
          htmlStr.Append("
  • ");
        }
        reader.Close();
        con.Close();
        HtmlContent.Text = htmlStr.ToString();
        }
    }
    File: Web.config