Archive for the ‘ASP.Net’ Category

Download this Tutorial Project

Please make comment is you think this articles helps you.

In Part 1 we saw how to create all 3 layers (UI, BLL, DAL). So in this project we are going to see how all three projects are going to interact with each other.

1. UI Layer – User will enter the User Name / Password to enter into our project.

2. BLL – We will check whether he entered both the User Name & password – if not we will show them an error like “Please enter the User Name” or  “Please enter the Password”.

3. DAL – If the user entered both the “User name & Password” then will check that User Name & Password in the database – whether its correct or not.

This is how all three projects will interact with each other.

Note: we are not going to concentrate on Login Page or any other pages design part.

Step 1:
Open the “Default.aspx” page. Place three Label Controls & two TextBox Controls. Name the textbox controls like “txtUserName” & “txtPassword”.

Place a new command button after those text box & name it as “cmdLogin”.

Now “Default.aspx” page will look like this:

<asp:Label runat=”server” ID=”lblError” Text=”"></asp:Label><br /><br /><br />

<asp:Label runat=”server” ID=”Label1″ Text=”User Name”></asp:Label>
<asp:TextBox runat=”server” ID=”txtUserName”></asp:TextBox> <br /><br />

<asp:Label runat=”server” ID=”Label2″ Text=”User Name”></asp:Label>
<asp:TextBox runat=”server” ID=”txtPassword”></asp:TextBox> <br /><br />

<asp:Button runat=”server” ID=”cmdLogin” Text=”Login” />

==================================================================

When you run this project (F5) the page will look like this in Browser:

Login Page

Step 2:
Now we should work on Database Access. First create one database called “SampleProject” in your MS SQL. Then create only one table named “MUser” with following fields.

1. UserID – Int
2. UserName – varchar(128)
3. UserPassword – varchar(256)

Then enter one record into the database table for testing purpose… like
UserName: admin
UserPassword: adminpass

Now we should write a stored procedure to get these Records from Database table. So the stored procedure will look like this.

CREATE PROC [dbo].[sp_User_Login]
@UserName varchar(128), @UserPassword varchar(256)
AS
BEGIN
SELECT
*
FROM
MUser
WHERE
UserName = @UserName
AND
UserPassword = @UserPassword
END

Step 3:
Now we need to code the Data Access Layer. We need to add these reference to our Data Access Layer.

Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Data.dll

Then add new Class file (.cs) called DAUser.cs to the DAL project. We are going to user this file to access our database.
Once we added these references (dll) & Class file (.cs) we need to write code to access the database. The DAL Code in DAUser.cs page will look like this below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace SampleProject.DataAccess
{
public class DAUser
{
public DataSet UserLogin(string UserName, string UserPassword)
{

//DBNameIs – is coming from web.config file – connectionStrings section
DataSet ds = new DataSet();
Database db = DatabaseFactory.CreateDatabase(”DBNameIs”);
DbCommand cmd = db.GetStoredProcCommand(”sp_User_Login”);
db.AddInParameter(cmd, “UserName”, DbType.String, UserName.Trim());
db.AddInParameter(cmd, “UserPassword”, DbType.String, UserPassword.Trim());
ds = db.ExecuteDataSet(cmd);
return ds;
}
}
}

Thats it!. Now Database is ready, as well as DAL is also ready.

Step 4:
Now we should write code to check whether the entered Username & Password is correct or not. We are going to write this code in Business Logic Layer (BLL).

Before this we need to Add DAL reference to BLL Layer, so that BLL layer can access the data from DAL layer (Hope you understand this step). So how to add this Reference??

In Solution Explorer in BLL layer Right click on the “Reference” & again click on “Add Reference” menu . It will show you a dialog box as shown below.

BLL - DAL Reference

Now click on Project tab & select the “SampleProject.DataAccess” project & click ok. Now we added DAL reference to BLL.

Our next step is to create the Logic. So now add a New class file to BLL project – named “BLUser.cs

Now write the login logic as below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SampleProject.DataAccess;
using System.Data;

namespace SampleProject.BusinessLogic
{
public class BLUser
{
public bool UserLogin(string UserName, string UserPassword)
{
//Data Access Layer Object.
DAUser objUser = new DAUser();

//Now we are getting details from Database.
DataSet ds = new DataSet();
ds = objUser.UserLogin(UserName, UserPassword);

//If the username & password matchs then Stored Procedure will return User Details
//So if we have any rows it means User entered Correct Username / Password.
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
return true;
}
else
{
return false;
}
}
}
}

That’s it. Now BLL is also ready.

Step 4:
Now we should get the username / password & pass those to BLL.

Add the reference to the BLL project from UI project. Once you added the reference open the “default.aspx” page add this line:
using SampleProject.BusinessLogic;

Then we need to write the code in Button click to check the given username / password. So it will be like this:

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 SampleProject.BusinessLogic;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void cmdLogin_Click(object sender, EventArgs e)
{
BLUser objUser = new BLUser();
if (objUser.UserLogin(this.txtUserName.Text, this.txtPassword.Text))
{
this.lblError.Text = “Logged In Successfully.”;
}
else
{
this.lblError.Text = “Invalid Username / password.”;
}
}
}

Now run the project & test with sample username / password. Download this tutorial project.

If you still have issues please let me know through comment. I will try to help you – even on skype chat.

ASP.Net/C# – Creating Business Logic Layer / Data Access Layer

Posted by nsrajesh On February - 12 - 2010

Download this Tutorial Project

Please make comment is you think this articles helps you.

How to create/use Business Logic Layer / Data Access Layer in ASP.Net project

Do you guys know how to create a Simple ASP.Net project with Business Logic Layer (BLL) & Data Access Layer (DAL) along with Presentation Layer or User Interface Layer (UI) – Simply ASP.Net Project…

Lets learn together.

First step is to Create an ASP.Net project (hope you guys know how to create ASP.Net Project :) ) – Lets name it as “SampleProject”.  While saving the project Save it in your wwwroot Folder. Mostly it will be in C:\Inetpub\wwwroot\.

Now you solution explorer will look like this.

DAL-BAL-Solution-Explorer

DAL-BAL-Solution-Explorer

Creating BLL

Our next step is to create the Business Logic Layer (BLL) & Data Access Layer (DAL) – From now on we will call it as BLL / DAL. For BLL & DAL layer we need to create DLL Files – using Class Library Projects. So just right click on your Project’s Solution file as show below.

Selecting Solution to add BLL & DAL

Selecting Solution to add BLL & DAL

Now select Windows in Project Type – left hand side of the dialog box. Then select Class Library in Templates – right hand side. Now give the project name as “SampleProject.BusinessLogic” – Save this project in wwwroot folder itself.

Now your project explorer will look like below.

solution-exporer-bal-asp

Creating DAL

Follow the same steps that you used to create the BLL  to create the DAL project and name the project as “SampleProject.DataAccess“.

This is how your solution explorer will look like now.

Solution Exporer with UI Layer, BLL, DAL

Using all three Layers

Now we have UI, BLL & DAL projects is in place. So how we are going to interact all these projects. Now we are going to create a login page where users can login.

And we are not going to use any ASP.Net controls like Login / Login View / Login Status etc… to create this login page. Also we are going to use Microsoft Enterprise Library to access the Database. If you don’t know Enterprise Library, please don’t worry – Its just like OLEDB.

Now, while the user logs in into our product our all projects (UI, BLL & DAL) will interact like below.

1. UI Layer – User will enter the User Name / Password to enter into our project.

2. BLL – We will check whether he entered both the User Name & password – if not we will show them an error like “Please enter the User Name” or “Please enter the Password”.

3. DAL – If the user entered both the “User name & Password” then will check that User Name & Password in the database – whether its correct or not.

This is how all three projects will interact with each other.

Please read the Part 2 of this tutorial

If you still have issues please let me know through comment. I will try to help you – even on skype chat.

VIDEO

Enter the video embed code here. Remember to change the size to 320 x 270 in the embed code.

TAG CLOUD

WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.

Sponsors

About Me

Hi all, my name is Rajesh & i am a full time developer who loves to work on Challenging Projects. I am specialized in ASP.Net, C#, PHP, MySQL & MS SQL. This Blog\'s goal is to provide tips/help to others. I will try to post most useful Articles once in a while.

Twitter

    Photos

    Activate the Flickrss plugin to see the image thumbnails!