Showing posts with label Multi Tier. Show all posts
Showing posts with label Multi Tier. Show all posts

Tuesday, June 10, 2008

.Net C# Multi Tier Application

The 3-Tier architecture has the following three tiers:

Data Tier
This tier consists of Database Servers. Here information is stored and retrieved. This tier keeps data neutral and independent from application servers or business logic. Giving data its own tier also improves scalability and performance.
Application Tier (Business Logic/Logic Tier)
The logic tier is pulled out from the presentation tier and, as its own layer, it controls an application’s functionality by performing detailed processing.
Presentation Tier
This is the topmost level of the application. The presentation tier displays information related to such services as browsing merchandise, purchasing, and shopping cart contents. It communicates with other tiers by outputting results to the browser/client tier and all other tiers in the network.
Lets take up creating a very simple multi tier application.

First we will see the Data Tier. Here I am using Sql Server 2005 as data store.



















Now we will create the Application Tier or the Business Tier. For this I am creating a Class Library Project.

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace ShalvinBusinessObject
{
public class BusinessObject
{
public DataTable GetCategories()
{
SqlConnection cnn;
SqlDataAdapter da;
DataSet ds = new DataSet();
cnn = new SqlConnection(@"Integrated Security=sspi;Initial Catalog=ShalvinPDBlog;Data Source=.\sqlexpress");
cnn.Open();
da = new SqlDataAdapter("spAllCategories", cnn);
da.Fill(ds, "Cat");
return ds.Tables["Cat"];
}
}

Goto Build, Build Solution for building the dll.

Creating the User Tier

Start a Windows Application Project.
Go to project, Add Reference, Select Browse and Navigate to the bin, release folder of the previously creating class library project. Select the dll.

Now you can access the dll from your project.



















using ShalvinBusinessObject;


BusinessObject bo;
private void Form1_Load(object sender, EventArgs e)
{
bo = new BusinessObject();
dataGridView1.DataSource = bo.GetCategories();
}

Here I am creating an Object of the Class in the previous dll and binding an dataGridView to the GetCategories method.

Wednesday, February 20, 2008

Asp.Net : Implementing Date Selection without CalendarExtender Ajax Control

Thought implementing Date Selection options with CalendarExtender Ajax Control is a breeze, many ASP.Net Programmers are still working with Asp.net 1.x.
With a bit of nostalgic tint let me blog on the code I used to write to implement the same functionality in Asp.Net 1.x.

I start out with placing three combo boxes for Day, Month and Year and fill the controls with valuues. Then catenated values of three comboxes will be committed to table.

Here is the code:

using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection cnn;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDayCombo();
FillMonthCombo();
FillYearCombo();
DataBind();
}
cnn = new SqlConnection("Integrated Security=sspi;Initial Catalog=Shalvin;Data Source=.\\sqlexpress");
cnn.Open();
}

protected void FillDayCombo()
{
for (int i = 1; i < 31; i++)
ddlDay.Items.Add(i.ToString());
}
protected void FillMonthCombo()
{
string[] strMonths = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec" };
ddlMonth.DataSource = strMonths;
}
protected void FillYearCombo()
{
for (int i = 1990; i < 2020; i++)
ddlYear.Items.Add(i.ToString());
}

protected void Button1_Click(object sender, EventArgs e)
{
string strDate = ddlDay.Text.ToString() + "-" + ddlMonth.Text.ToString() + "-" + ddlYear.Text.ToString();
Response.Write(strDate);
cmd = new SqlCommand("Insert into DateTest values ('" + strDate + "')", cnn);
cmd.ExecuteNonQuery();
Response.Write("Record Saved");
}
}


MultiTier

Now let's implement the same solution in Multi tier scenario.
I am creating a class called DBConnect.

public class DbConnect
{
public ArrayList GetDays()
{
ArrayList alDays = new ArrayList();
for (int i = 1; i < 31; i++)
alDays.Add(i.ToString());
return alDays;
}
public string[] GetMonths()
{
string[] strMonths = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec" };
return strMonths;
}
public ArrayList GetYears()
{
ArrayList alYears = new ArrayList();
for (int i = 1990; i < 2020; i++)
alYears.Add (i.ToString());
return alYears;
}

Coming back to the form I am instantiating the DbConnect class and callilng its methods.
DbConnect dbc = new DbConnect();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlDay.DataSource = dbc.GetDays();
ddlMonth.DataSource = dbc.GetMonths();
ddlYear.DataSource = dbc.GetYears();
DataBind();
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
string str = ddlDay.Text + "-" + ddlMonth.Text + "-" + ddlYear.Text;
Response.Write(str);
}


Happy Programming
Shalvin@gmail.com

Monday, January 7, 2008

.Net Multi-Tier Application

This blog takes up the issue of creating multi tier application with VB.Net (Windows Forms) and C# (Asp .Net).

Start up with creating two stored procedures.

create procedure spAllCategories
as
select * from Categories
go


create procedure spInsertCategories (@pCategoryName varchar(40), @pDescription varchar(60))
as
insert into Categories values (@pCategoryName, @pDescription)
go

Create an new class Library project called ShalvinLib and add two methods that will call the stored procedures. This is going to act as the business tier.

Imports System.Data.SqlClient
Public Class BusinessLayer
Public Shared Function GetCategories() As DataTable
Dim cnn As New SqlConnection("Integrated Security=sspi;Initial Catalog=Northwind;Data Source=.\sqlexpress")
cnn.Open()

Dim ds As New DataSet
Dim da As New SqlDataAdapter("spAllCategories", cnn)
da.Fill(ds, "Cat")
Return ds.Tables("Cat")
End Function

Public Shared Sub InsertCategories(ByVal mCategoryName As String, ByVal mDescription As String)
Dim cnn As New SqlConnection("Integrated Security=sspi;Initial Catalog=Northwind;Data Source=.\sqlexpress")
cnn.Open()
Dim cmd As SqlCommand

cmd = New SqlCommand("spInsertCategories " + mCategoryName + ", " + mDescription, cnn)

cmd.ExecuteNonQuery()

End Sub

End Class

C# and Asp.Net
Here instead of using Windows Forms I am opening a Asp.Net Application and Instead of starting a new class Library I am adding a Class to the existing project.

web.config




using System.Data.SqlClient;
public static DataTable GetCategories()
{
SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings.Get("Cnn"));
SqlDataAdapter da = new SqlDataAdapter("spAllCategories", cnn);
DataSet ds = new DataSet();
da.Fill(ds, "Cat");
return ds.Tables["Cat"];
}

public static void InsertCategories(string mCategoryName, string mDescription)
{
SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings.Get("Cnn"));
cnn.Open();
string strSql = "spInsertCategory '" + mCategoryName + "', '" + mDescription + "'";
SqlCommand cmd = new SqlCommand(strSql , cnn);
cmd.ExecuteNonQuery();
}


Having created the class library dll, we can proceed to create a windows forms application.
Set a reference to the class library. Create the visual interface and invoke the methods of class library.

Dim bl As New ShalvinLib.BusinessLayer
Private Sub frmShowCategories_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.DataSource = bl.GetCategories
End Sub

C# and Asp .Net
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = BusinessLayer.GetCategories();
DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
BusinessLayer.InsertCategories("Books", ".Net Books");
Response.Write("Record Saved");
}

Happy programming