ADO Net Database ASP.Net Tutorial

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



    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.Common;
using System.Text;
using System.Web.Configuration;
public partial class ProviderAgnosticCode : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    string factory = WebConfigurationManager.AppSettings["factory"];
    DbProviderFactory provider = DbProviderFactories.GetFactory(factory);
    DbConnection con = provider.CreateConnection();
    con.ConnectionString = WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
    
    DbCommand cmd = provider.CreateCommand();
    cmd.CommandText = WebConfigurationManager.AppSettings["employeeQuery"];
    cmd.Connection = con;
    con.Open();
    DbDataReader 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