LINQ With 3 Layer Architecture (Insert Data Into Database)





LinqLayer1.gif

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)

LinqLayer2.gif

  • Create a web site with the name LINQ_WithThreeLayer.

  • Go to File menu, select New and select Website.

    LinqLayer3.gif

  • Select ASP.NET Empty Website.

    LinqLayer4.gif

  • Give the application a name (LINQ_WithThreeLayer).

    LinqLayer5.gif

Data Access Layer:

  • Go to the Solution Explorer, select the application, right-click select "Add new Item".

    LinqLayer6.gif

  • Select LINQ to SQL Classes Template.

    LinqLayer7.gif

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

    LinqLayer8.gif
     

  • Click Yes.

    LinqLayer9.gif

  • One App_Code folder is created in Solution Explorer.

  • And OR-Designer window displayed.

    LinqLayer10.gif

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

    LinqLayer11.gif
     

  • Select Microsoft SQL Server and click Continue.

    LinqLayer12.gif

  • 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.

    LinqLayer13.gif

  • 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.

    LinqLayer14.gif

  • 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.

    LinqLayer15.gif

    LinqLayer16.gif

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

    LinqLayer17.gif


    Select Class.

    LinqLayer18.gif

    LinqLayer19.gif

    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).

    LinqLayer20.gif

    LinqLayer21.gif
     

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

    LinqLayer22.gif

    LinqLayer23.gif

    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!";
            }

        }
    }

     

 

Up Next
    Ebook Download
    View all
    Learn
    View all