How to create 3 tier application using LINQ


As you know that in 3 tier architecture there are three layers

  1. User interface layer. (Is our Form in Windows application and .aspx page in Web application)
  2. Data Access layer. (Which provides interface between Business logic layer and Database)
  3. Business Logic layer.(Which stores your application logic)

1.gif

Fig. 3 Tier Architecture 

Now we start how to create 3 tire architecture.

Step 1:
Create blank Solution name 'test'.

Step 2:
Add new Class Library project. Write name like 'BusinessLogiclayer'.

Step 3:
Add new website. Write name like 'UserInterfacelayer'.

Step 4:
Add new Class Library project. Write name like 'DataAccesslayer'.





Step 5:
Now add reference in to BusinessLogiclayer  

2.gif

Step 6:
And last Add reference to UserInterfacelayer

3.gif

Here we add two references Businesslogiclayer and DataAccesslayer. So your window will look like this,

4.gif

Now create Database name testLINQ and create a table name REGISTER.

CREATE TABLE
[dbo].[REGISTER](
          [ID] [int] IDENTITY(1,1) NOT NULL,
          [FNAME] [nvarchar](50) NOT NULL,
          [LNAME] [nvarchar](50) NOT NULL,
          [EMAILID] [nvarchar](max) NOT NULL,
          [PASSWORD] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_REGISTER] PRIMARY KEY CLUSTERED
(
          [ID] ASC
) WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Step 7:
Create a form Register.aspx

<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="Register" %>
<!
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:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                 <table align="center">
                         <tr>
                           <td>
                    <asp:Label ID="lblfname" runat="server" Text="First Name:"></asp:Label></td>
                           <td>
                    <asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
                               <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                                   ControlToValidate="txtfname" ErrorMessage="Firstname Required !">*</asp:RequiredFieldValidator>
           
               </td>
                         </tr>
                         <tr>
                              <td>
                    <asp:Label ID="lbllname" runat="server" Text="Last Name:"></asp:Label>
                </td>
                              <td>
                    <asp:TextBox ID="txtlname" runat="server"></asp:TextBox>
                                  <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                                      ControlToValidate="txtlname" ErrorMessage="Last Name required !">*</asp:RequiredFieldValidator>
                </td>
                        </tr>
                         <tr>
                              <td>
                    <asp:Label ID="lblemailid" runat="server" Text="Emailid:"></asp:Label>
                </td>
                              <td>
                    <asp:TextBox ID="txtemailid" runat="server"></asp:TextBox>
                                  <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
                                      ControlToValidate="txtemailid" ErrorMessage="Email id required !">*</asp:RequiredFieldValidator>
                                  <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
                                      ControlToValidate="txtemailid" ErrorMessage="Invalid Email id !"
                                      ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
                </td>
                         </tr>
                         <tr>
                              <td>
                    <asp:Label ID="lblpassword" runat="server" Text="Password:"></asp:Label>
                </td>
                              <td>
                    <asp:TextBox ID="txtpassword" runat="server" TextMode="Password"></asp:TextBox>
                                  <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
                                      ControlToValidate="txtpassword" ErrorMessage="Password required !">*</asp:RequiredFieldValidator>
                                  <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
                                      ControlToValidate="txtpassword" ErrorMessage="Invalid Password !"
                                      ValidationExpression="\w{6,10}">*</asp:RegularExpressionValidator>
                </td>
                         </tr>
                         <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Btnregister" runat="server" onclick="Btnregister_Click"
                        Text="Register" />
                             </td>
            </tr>
               </table>   
            </ContentTemplate>
        </asp:UpdatePanel>      
</
div>
    </form>

</
body>
</
html>

Page will look like this in browser

5.gif

Step 8:
In DataAccesslogiclayer add LINQ TO SQL class name test.dbml

Step 9:
Create Stored procedure sp_REGISTER.

-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
-- =============================================
-- Author:     
          Jayendrasinh Gohil
-- Create date: 30/07/2009
-- Description:          STORED PROCEDURE FOR REGISTER TABLE
-- =============================================

ALTER PROCEDURE
sp_REGISTER
          -- Add the parameters for the stored procedure here
          (
   @Task INT =0,
   @FNAME nvarchar(50)='',
   @LNAME nvarchar(50) = '',
   @EMAILID nvarchar(MAX) = '',
   @PASSWORD nvarchar(50)
)

AS
IF
(@Task = 1)
 BEGIN
   INSERT INTO [testLINQ].[dbo].[REGISTER]
           ([FNAME]
           ,[LNAME]
           ,[EMAILID]
           ,[PASSWORD])
     VALUES
           (
             @FNAME,
             @LNAME,
             @EMAILID,
             @PASSWORD
           )

END

Here we take @Task variable for running multiple SQL statement run in task wise. Pass @Task = 1 for insert, 2 for delete so on.

Step 10:
In Businesslogic layer, create class REGISTER.cs like this

#region
"Public Using"
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Data.Linq;
#endregion
#region
"Private Using"
using
DataAccesslayer;
#endregion 

namespace BusinessLoggiclayer
{
    public class REGISTER
    {

        #region
"Private variable"
        string _FNAME;      
        string _LNAME;     
        string _EMAILID;      
        string _PASSWORD;       

        #endregion 

        #region "Public Property"
        public string FNAME
        {
            get { return _FNAME; }
            set { _FNAME = value; }
        }
        public string LNAME
        {
            get { return _LNAME; }
            set { _LNAME = value; }
        }
        public string EMAILID
        {
            get { return _EMAILID; }
            set { _EMAILID = value; }
        }
        public string PASSWORD
        {
            get { return _PASSWORD; }
            set { _PASSWORD = value; }
        }

        #endregion

        #region "Public Method"
        public void Insert()
        {
            using (testDataContext db = new testDataContext())
            {
                db.sp_REGISTER(1, _FNAME, _LNAME, _EMAILID, _PASSWORD);
            }
        }

        #endregion

    }
}

Step 11:
Write code in Register.aspx.cs

using
System;
using
System.Collections;
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; 

#region "Private Using"
using
BusinessLoggiclayer;
#endregion 

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
        }
    }
    protected void Btnregister_Click(object sender, EventArgs e)
    {
        REGISTER r = new REGISTER();
        r.FNAME = txtfname.Text;
        r.LNAME = txtlname.Text;
        r.EMAILID = txtemailid.Text;
        r.PASSWORD = txtpassword.Text;
        r.Insert();
    }
}

Up Next
    Ebook Download
    View all
    Learn
    View all