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

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.





