Friday 20 January 2012

3 tier Architecture of ASP.NET

    
  • The configuration of ASP.NET is managed by information stored in XML-format in a configuration file (Web.Config).
  • The cache allows for improved performance of ASP.NET, as the most commonly requested pages would be served from
  • the ASP.NET cache.State management services for ASP.NET are provided by the ASP.NET state service.
  • The .NET Framework provides the Common Language Runtime (CLR) , which compiles and manages the execution of ASP.NET
  • code, and the class libraries, which offer prebuilt programmatic functionality for Web Forms, XML support, and exception handling.
  • ADO.NET provides ASP.NET with connections to databases.

3-Tier Architecture in ASP.NET with C#


using System;

using System.Data;

using System.Configuration;

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.OleDb;


public class PersonDAL
{
    
  string connStr = ConfigurationManager.ConnectionStrings["connectionstring"].ToString();
    public PersonDAL()
    {
   }
    
    public int Insert(string name, string address, int age)
    {

        OleDbConnection conn = new OleDbConnection(connStr);

        conn.Open();

  OleDbCommand dCmd = new OleDbCommand("insert into insertrecord values(@P,@q,@r)",conn);
        try
        {
            dCmd.Parameters.AddWithValue("@p", name);
            dCmd.Parameters.AddWithValue("@q", address);
            dCmd.Parameters.AddWithValue("@r", age);
            return dCmd.ExecuteNonQuery();

        }

        catch
        {

            throw;

        }

        finally
        {

            dCmd.Dispose();

            conn.Close();

            conn.Dispose();

        }

    }
    }
 
 
Code for .cs file

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;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Lets validate the page first

        if (!Page.IsValid)

            return;

        int intResult = 0;

        PersonBAL pBAL = new PersonBAL();

       // Instantiate the object we have to deal with

        string name = txtname.Text;
        string address = txtaddress.Text;
        int age = Int32.Parse(txtAge.Text);
        try
        {
           intResult = pBAL.Insert(name, address, age);
            if (intResult > 0)

               lblMessage.Text = "New record inserted successfully.";

            else

   lblMessage.Text = "FirstName [<b>" + txtname.Text + "</b>] 
                      alredy exists, try another name";

        }
        catch (Exception ee)
        {

            lblMessage.Text = ee.Message.ToString();

        }

        finally
        {

            pBAL = null;

        }        
    }
}
Presentation Layer

Code for .aspx page

<%@ 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>
    <style type="text/css">
        .style1
        {
            width: 100%;
            height: 215px;
            background-color: #FFFFCC;
        }
        .style2
        {
            width: 271px;
        }
        .style3
        {
            width: 271px;
            height: 44px;
        }
        .style4
        {
            height: 44px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div style="background-color: #008000; height: 253px;">
    
        <table class="style1">
            <tr>
                <td colspan="2">
                                      ADD RECORDS&nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;&nbsp;&nbsp;&nbsp;Name</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp&nbsp; 
                    Address&nbsp;</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Age&nbsp;</td>
                <td class="style4">
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
                        Text="Save Record" />
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>   
    </div>
    </form>
</body>
</html>
 
 

No comments :