
Step 1: (Back End)
When developing an application, first we prepare a database.
Create Databse:
Create dataBase LINQ_WithThreeLayer
use LINQ_WithThreeLayer
Create table:
create table tbl_Registration
(
Reg_Id int IDENTITY(1,1) primary key,
First_Name varchar(50) NULL,
Last_Name varchar(50) NULL,
UserName varchar(50) NULL,
Password varchar(50) NULL,
)
Create stored Procedure:
Create Proc [dbo].[SP_Registration]
(
@FName varchar(50),
@LName varchar(50),
@UName Varchar(50),
@Password varchar(50)
)
As
Begin
declare @Result int
if not exists(select UserName from tbl_Registration where UserName=@UName)
begin
insert into tbl_Registration(First_Name,Last_Name,UserName,Password)
values(@FName,@LName,@UName,@Password)
set @Result=1
return @Result
end
else
begin
set @Result=0
return @Result
end
End
Step 2: (Front End)
-
Create a web site with the name LINQ_WithThreeLayer.
-
Go to File menu, select New and select Website.

-
Select ASP.NET Empty Website.

-
Give the application a name (LINQ_WithThreeLayer).

Data Access Layer:
-
Go to the Solution Explorer, select the application, right-click select "Add new Item".

-
Select LINQ to SQL Classes Template.

-
Give the name for the class "LinqWithThreeLayer" and click add.

-
Click Yes.

-
One App_Code folder is created in Solution Explorer.
-
And OR-Designer window displayed.

-
Go to Server Explorer, and click Connect to database.

-
Select Microsoft SQL Server and click Continue.

-
Give Server name (Client-server) / Machine Name(1-tier), select SQL Server Authentication, give the Database Name and click Test connection and finally Click OK.

-
Go to the Serve rExplorer and drag tables and drop in OR-Designer to left side window, and drag stored procedures and drop to the right side window.
-
Then save OR-Designer window.

-
After Saving the OR-Designer window, automatically the Data Access Layer class was prepared. And in the web.config file the Connectionstring is created.
<connectionStrings><add name="LINQ_WithThreeLayerConnectionString"
connectionString="Data Source=dotnet16;Initial Catalog=LINQ_WithThreeLayer;
User ID=sa;
Password=123"
providerName="System.Data.SqlClient" />
</connectionStrings>
Business Access Layer:
-
Go to Solution Explorer and select App_Code; select New Folder. Folder name BAL.


-
Select BAL and right click; select Add New Item. Again we can select Class Template, and class name is clsRegistration.

Select Class.

clsRegistration class code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;/// <summary>
/// Summary description for clsRegistration
/// </summary>
public class clsRegistration
{
LinqWithThreeLayerDataContext obj = new LinqWithThreeLayerDataContext();
public clsRegistration()
{
//
// TODO: Add constructor logic here
//
}
public int InsertRegistrationDetails(string FName, string Lname, string UserName,
string
Password)
{
int res = obj.SP_Registration(FName, Lname, UserName, Password);
return res;
}}
Presentation Layer:
-
Go to the Solution Explorer and select application and right click and select Add New Folder. And give it the name PL (Presentation layer).


-
Go to PL folder and select and right-click, select Add New Item, and select Webform template; give it the name frmRegistration.


Code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class ThreeLayer_Linq : System.Web.UI.Page
{
clsRegistration obj = new clsRegistration();
protected void Page_Load(object sender, EventArgs e)
{}
protected void btnRegister_Click(object sender, EventArgs e)
{
string Fname = Convert.ToString(txtFName.Text);
string Lname = Convert.ToString(txtLName.Text);
string Uname = Convert.ToString(txtUName.Text);
string Password = Convert.ToString(txtPassword.Text);int res = obj.InsertRegistrationDetails(Fname, Lname, Uname, Password);
if (res > 0)
{
lblResult.Text = "Registration details are inserted Successfully!";
}
else
{
lblResult.Text = "Not inserted!";
}}
}

Manish Kumar ChoudharyPosted Nov 23, 2014, 10:53 PM
Nice one..
Dharmendra KumarPosted Nov 23, 2014, 7:41 AM
thANkx sir.....
Dharmendra KumarPosted Nov 23, 2014, 7:40 AM
Good one......
Sanjay KumarPosted Dec 14, 2013, 7:08 AM
Nice
ketan italiyaPosted Dec 11, 2013, 6:14 AM
woww...superbb..
Vijay YadavPosted Mar 13, 2012, 11:13 PM
Hi Vamshi, Good post.. Please keep it up... :)
Mahesh BabuPosted Nov 5, 2011, 4:39 PM
I Learned some new points from this article
Nico VanHaastereditedPosted Jun 23, 2011, 9:56 PMEdited Jun 23, 2011, 9:56 PM
Let me start off by saying this is a very informative article and a great start for users to get involved with Linq 2 Sql. However I am not noticing much distinction between your layers, typically a 3 layer design is decoupled objects that can be referenced and self maintained. That being said, great effort put into this article.
Former memberPosted Jun 23, 2011, 12:22 AM
Nice one
Shirsendu NandiPosted Jun 22, 2011, 1:01 PM
good one dude..
surender bhyanPosted Jun 22, 2011, 6:30 AM
Wonderful explanation with picture.thanks a lot sir
Dinesh BeniwalPosted Jun 22, 2011, 3:58 AM
Thanks for sharing..
Raju KatarePosted Jun 22, 2011, 3:20 AM
Nice Explanation...