How to create BAL and DAL layers and how to give reference to access them?

Posted by Iluvdotnetfunda under C# on 6/29/2012 | Points: 10 | Views : 5807 | Status : [Member] | Replies : 4
Hello Friends,

I need to create BAL , DAL, BO layers in my application and how to give reference to them.
Please any one help me to do this .
I need step by step process.

Thanks
iluvdotnetfunda




Responses

Posted by: Kamalakanta.Nayak09 on: 6/29/2012 [Member] Starter | Points: 25

Up
0
Down
In side the App_Code you can create three files like BusinessAccessLayer.cs,DataAccessLayer.cs,BusinessObjectLayer.cs
Suppose you add Region Information to your Database
You first take the Entry page of Region.aspx

1. Inside the Region.aspx.cs
protected void btnSubmit_Click(object sender, EventArgs e)
{

if (!Page.IsValid)
return;
int Result = 0;

BusinessAccessClass bao = new BusinessAccessClass();
Region objRegion = new Region();

objRegion.propRegionName = this.txtRegionName.Text.Trim();
objRegion.propStateID = Int32.Parse(this.ddlState.SelectedValue);

try
{

Result = bao.InsertRegionData(objRegion);

if (Result > 0)
{

//to avoid duplicate record insertion
Response.Redirect(Request.Url.ToString(), false);
this.lblMessage.Text = bao.InsertMessage();
Response.Redirect("~/Modules/RegionView.aspx");
}
}
catch (Exception ex)
{
this.lblMessage.Text = ex.Message.ToString();

}
finally
{
objRegion = null;
bao = null;
}

}
2.Pass the Input value to BO class
property class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


public class Region
{
public Region()
{
//
// TODO: Add constructor logic here
//
}

string RegionName = string.Empty;
int StateID = 0;

public string propRegionName
{
get { return RegionName;}
set { RegionName = value; }
}

public int propStateID
{
get { return StateID; }
set { StateID = value; }


}
}
3. Inside BusinessAccessLayer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

public class BusinessAccessClass
{
public BusinessAccessClass()
{
//
// TODO: Add constructor logic here
//
}

public int InsertRegionData(Region region)
{
SqlParameter RegionName = new SqlParameter("@RegionName", region.propRegionName);
SqlParameter StateID = new SqlParameter("@StateID", region.propStateID);
SqlParameter[] DBRegionValues = new SqlParameter[2] { RegionName, StateID };
DataAccessClass dao = new DataAccessClass();
int result = dao.ExecuteNonQuery("spInsertRegionData", DBRegionValues);
return result;
}

4.In side the DAL Class

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public class DataAccessClass
{
public string Conne = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

public int ExecuteNonQuery(string StoredProc, SqlParameter[] paramCollection)
{
int effectedRows = 0;
SqlCommand command = new SqlCommand(StoredProc);
Conne .Open();
command.Connection = this.Conne ;
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddRange(paramCollection);
effectedRows = command.ExecuteNonQuery();
return effectedRows;

}
}
In this way you can write the code

Thanks,
K.K

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajkatie on: 7/1/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Kindly refer following links
http://msdn.microsoft.com/en-us/library/aa581779.aspx

Rajesh Patel
.Net Trainer(MSCD | MCT | MS)
Brainbench (Asp.Net 4.5 and C# 5.0)

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 7/2/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

here is a nice Article about 3-Tier architecture by SheoNarayan with sample code
Refer this

http://www.dotnetfunda.com/articles/article71.aspx

Thanks
and
Regards




R D Patel

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muralidosscm on: 7/3/2012 [Member] Starter | Points: 25

Up
0
Down
Add the DAL, BAL file in the project, then add .cs file............ call the BAl from the code behind, then Call DAL from BAl............ try this..........

Regards
Muralidoss M

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response