I have created one project as shown above name as 3-tier.
For implementation of three tier architecturewe need to separate all three
layers, so I have created business layer
(AddBAL.cs) , Data access layer (AddDAL.cs) and presentation layer (Default.aspx)
separately. We need to just reference BAL in presentation layer.
Data Access Layer (DAL) :
AddDAL.cs
using
System;
using System.Data;
using System.Configuration;
public class AddDAL
{
string conStr = ConfigurationManager.ConnectionStrings
["ConnectionStringName"].ToString();
// Add records into database
public int AddMethod(string fName, string lName)
{
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand Cmd = new SqlCommand("AddData", conn);
Cmd.CommandType = CommandType.StoredProcedure;
try
{
Cmd.Parameters.AddWithValue("@fName", fName);
Cmd.Parameters.AddWithValue("@lName", lName);
return Cmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
Cmd.Dispose();
conn.Close();
conn.Dispose();
}
}
-->
Business Access Layer (BAL):
AddBAL.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
// Summary description for PersonBAL3
public class AddBAL
{
// Add records into database
public int AddMethod(string fName, string lName)
{
AddDAL ObjAdd = new AddDAL ();
try
{
return ObjAdd. AddMethod (fName, lName);
}
catch
{
throw;
}
finally
{
ObjAdd = null;
}
}
}
Presentation Layer :
Default.aspx
Default.aspx.cs
protected void AddRecords(object sender, EventArgs e)
{
int intTotal = 0;
//insert records
AddBAL ObjBAL = new AddBAL ();
string firstName = txtFName.Text;
string lastName = txtLName.Text;
try
{
intTotal = ObjBAL.AddMethod(fName, lName)
}
catch (Exception Ex)
{
lblMessage.Text = Ex.Message.ToString();
}
finally
{
ObjBAL = null;
}
}
Simple & Grateful for beginers.
ReplyDeleteThank you.
Kajan
really very simple and usefull...
ReplyDeleteone doubt: what I have to give in the place "AddData" in AddDal. Is it SQL command? please provide an eg. Thanks in advance.
one more doubt is
ReplyDeleteyou have given
intTotal = ObjBAL.AddMethod(fName, lName)
It is firstname and last name instead of fName and lName respectively right?
You never used fName and lName in the code anywhere?
since I am new in dot net can you pls write simple stored procedure also?
ReplyDeletethanks in advance.
ok i will write on stored proc, give me some time....
ReplyDeletewhen I create AddBAL.cs class, the below line willbe created automatically
ReplyDeletepublic class AddBAL
{
public AddBAL()
{
//
// TODO: Add constructor logic here
//
}
}
where should i write this code this AddMethod function.
public int AddMethod(string firstName, string lastName)
{
AddDAL objDAL = new AddDAL();
try
{
return objDAL.AddMethod(firstName, lastName);
}
catch
{
throw;
}
finally
{
objDAL = null;
}
}
in
public class AddBAL directly or inside public AddBAL() ??
..
Please write a description...
public class AddBAL
ReplyDelete{
//this is constructor which creates every time when you create class....it is use to define or declare variable of class...
public AddBAL()
{
//
// TODO: Add constructor logic here
//
}
public int AddMethod(string firstName, string lastName)
{
AddDAL objDAL = new AddDAL();
try
{
return objDAL.AddMethod(firstName, lastName);
}
catch
{
throw;
}
finally
{
objDAL = null;
}
}
before writing the stored procedure..could you please write another way to write data in access database.
ReplyDeleteI created one access database with one table 'tab_Name' with two fields fname and lname.
I used sql connection to connect access db. can you pls modify the DAL according to that.
This code wont insert data.!
ReplyDeleteThis is not for inserting data...it is just example of how to use three tier architecture.
ReplyDeleteplease add the tables or stored procedures what you created to this program...
ReplyDeleteWhen i try to deploy this 3-Tier application,i'm getting error at bal object that is created inside of code behind page.S0, please give me solution for this.send me solution to 'info2smanohar@gmail.com'
ReplyDeleteThank you very much for the example. I tried some logic in my application and it worked fine. Whatever I have found all over the net were a little bit complex. This is a way too easy for a beginner. Thanks a lot Jatin..
ReplyDeletethanks for a fine working code for a beginner..
ReplyDeletewhere is ENTITY LAYER
ReplyDelete
ReplyDeleteIt is really a great work and the way in which u r sharing the knowledge is excellent.
Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. Dot Net Training in chennai | Dot Net Training institute in velachery
ReplyDeleteIt is really a great work and the way in which u r sharing the knowledge is excellent.
Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. dot net training and placement in chennai | dot net training institute in velachery
This comment has been removed by the author.
ReplyDelete"Great blog created by you. I read your blog, its best and useful information. You have done a great work. Super blogging and keep it up.php jobs in hyderabad.
ReplyDelete"