6b. Create a web application to display records by using database.

Program:



Default.aspx

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Label ID="Label1" runat="server" Text="Customer details:"></asp:Label>
        <br />
        <br />
        <asp:Label ID="Label2" runat="server"></asp:Label>
   
    </div>
    <p>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="Display Record" />
    </p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    </form>
</body>
</html>





Default.aspx.cs

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection sqlcon =new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\user\Documents\Visual Studio 2008\WebSites\WebSite1\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //string connStr =ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        //SqlConnection sqlcon =new SqlConnection(connStr);
        SqlCommand cmd =new SqlCommand("Select ID, Name from student", sqlcon);
        sqlcon.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        while(reader.Read())
        {
            Label1.Text += reader["ID"].ToString() + " "+ reader["Name"].ToString() +"<br>";
        }
        reader.Close();
        sqlcon.Close();
    }

}

Comments