Recent Posts

Wednesday, November 19, 2008

This article focuses on explaining how to code a three tier asp.net web application visual basic. Here I will be explaining how you can bind an object to the data grid and display the data from the database just by adding the references to the business logic and data access dlls.

Ok first open a web project in visual studio 2005 and in Default.aspx add a Gridview and name it grdStudents

Now you need to add the references to the business tier dlls and data tier dlls.

Thursday, November 6, 2008

This article focuses on explaining how to code a three tier application usingvisual basic .net . Here I will be explaining each an every step from creatingnew sql server database table to running the vb program going through most ofthe vb code in the application and also providing some links that I find usefulfor the learners.In this visual basic net tutorial, I will be mainly focusingthe beginners to mid level .net programmers so I will try to expalin each andevery detail as I can but even that if I missed something don't hesitate to posta comment on this blog article. I hope this will be a good vb tutorial forwhoever is interested in it and this application can be easily extended to aspnet three tier application for web developers.

Now lets start (Note: Source code of this tutorial is available at the end ofthis article)

Step 1

Let's first create two tables in the sql server database with the following sqlquery:

(Note: You can create the tables yourself in Microsoft Sql Server ManagementStudio and it looks like the image shown below)

Now here you have to create a Foreign key constraint to classID (If you don'tknow what foreign key is then you can (Learn Foreign key)

Step 2

Now create a window project in visual basic and name it ThreeTierVB and saveyour project and solution in your desired Path.

Add Two folders in the project by right clicking the ThreeTierVB on yoursolution explorer andAdd --> New Folder and name themFormsGlobalClasses

Right click the forms Folder and add a windows form and name it frmStudent thenadd the following components in the form and name them accordingly as follows:

1 comboboxname:cboClass

4 command Buttonsname: cmdNewText: New Class

name: cmdClassSaveText: Save Class

name: cmdSaveText: Save

name: cmdExitText: Exit

2 text boxesname: txtClassName

name: txtStudentName

1 Datagridview

name: grdStudents

A quick snapshot of my form is shown below:

(add the labels and name the labels appropriately and also you can place the controls wherever you like them comfortable)

Step 3

Now before going to the coding lets create the three tier structure so that itwould be easier to understand for beginner programmers.

1. Add a new Class library to the solution by going toFile --> Add --> New project

2. Select the class library and name it ThreeTierVB.BusinessLogic andadd two folders BLLClasses and GenericClasses as explained on step 2

3. Similarly add another class library as above and name it ThreeTierVB.DataAccessand add three folders and name them DataClasses, GlobalClasses and Modules

4. Similarly add another class library as above and name it ThreeTierVB.Infoand add a folder and name it InfoClasses

Now your solution Explorer should look like the following

Step 4

Now we have got 4 projects in our solution Explorer. Lets make the idea clearNow here we are making DataAccess , BusinessLogic and GuI the three tiers andInfo is the info for all the three tiers, so we have to be clear on what getaccess to what.

Add the references to the projects

1. ThreeTierVB which is our presentation tier and this needs to know the infoand business logic but it doesn't worry about Dataaccess.

For this right click on ThreeTierVB project and Addreference in the add reference screen go to projects and select ThreeTierVB.Businesslogic and ThreeTierVB.Info by clicking ctrl and click ok button.

2. Similarly Businesslogic only needs to know the DataAccess and info so as donein 1 add references to ThreeTierVB.DataAccess and ThreeTierVB.Info.

3. Now in DataAccess project just add ThreeTierVB.Info as a reference.

Step 5

Add the required classes in the projects to run the application now as we haveset up all the necessary references.

1. In ThreeTierVB.Info add two classes and name them as ClassInfo and StudentInfo in ClassInfo Write the following code:

Public Class ClassInfo

#Region "Private Member Declarations"

Private mClassId As IntegerPrivate mClassName As String

Private mAction As Byte

#End Region

#Region "Public Properties"

Public Property Action() As Byte Get Return mAction End Get Set(ByVal value As Byte) mAction = value End SetEnd Property

Public Property ClassID() As Integer Get Return mClassId End Get Set(ByVal value As Integer) mClassId = value End SetEnd Property

Public Property ClassName() As String Get Return mClassName End Get Set(ByVal value As String) mClassName = value End SetEnd Property

#End Region

End Class

In StudentInfo Write the following code:

Public Class StudentInfo

#Region "Private Member Declarations"

Private mStudentID As IntegerPrivate mStudentName As StringPrivate mClassID As Integer

Private mAction As Byte

#End Region

#Region " Public Properties"

Public Property Action() As Byte Get Return mAction End Get Set(ByVal value As Byte) mAction = value End SetEnd Property

Public Property StudentID() As Integer Get Return mStudentID End Get Set(ByVal value As Integer) mStudentID = value End SetEnd Property

Public Property ClassID() As Integer Get Return mClassID End Get Set(ByVal value As Integer) mClassID = value End SetEnd Property

Public Property StudentName() As String Get Return mStudentName End Get Set(ByVal value As String) mStudentName = value End SetEnd Property#End Region

End Class

These two classes will act as objects of student and class throughout thesolution.Now your Solution Explorer will look like the following:

2. In ThreeTierVB.DataAccess

In DataClasses folder add ClasDB and StudentsDB classes and in GlobalClassesfolder add sqlHelp class and add a module modDB in modules folder.I will just provide the code below for this tutorial but for full details on howto access data from the database you can check How to insert record in SQLserver database from VB.Net?

Now in SQLHelp class add the following code:

Imports System.Data.SqlClient

Public NotInheritable Class SqlHelp

'Since this class provides only static methods, make the default constructor private to prevent'instances from being created with "new SqlHelper()".

Dim conn As SqlConnectionDim cmd As SqlCommandDim isTran As BooleanPrivate trans As SqlTransactionDim connStr As String

Private Function Connect() As Boolean Dim bln As Boolean ' Try If conn Is Nothing Then bln = ReadDatabseConfig(strDBConfFile) If bln = True Then conn = New SqlConnection(connStr) End If End If If conn.State = ConnectionState.Closed Then conn.Open() End If

Exit Function

End Function

Public Sub BeginTransaction()

If isTran Then Return If conn.State = ConnectionState.Closed Then conn.Open() End If trans = conn.BeginTransaction() isTran = TrueEnd Sub

' The function below will Delete the row of the class table with the providedclass ID. But to do this we have to first delete the related students in theclass because class has relation with the student table, this is the one of theadvantages of relational database.

Public Function DeleteClass(ByVal ClassID As Integer) As Boolean Dim strSql As String Dim objdb As New SqlHelp

In BLLClasses folder add classBll and studentsBll classesand in classBLL add the following code:

Public Class classBLL' This class now just acts as a bridge between the presentation layer and thedata access layer but later as the application grows most of the business casesand business level logic can be implemented in this layer.

Public Function GetClass() As ThreeTierVB.Info.ClassInfo() Dim db As New ThreeTierVB.DataAccess.classDB Return db.GetClass()End Function

Now we have completed all the Business logic and Dataaccess coding and came backto the form that we have left before.Add the following code to the form: (Note if error you can copy individualfunctions and subs and paste it in your form code page accordingly)

End SubPrivate Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles cmdExit.Click Application.Exit()End Sub

End Class

Now you can run the application and test, if you find errors try debugging it asenough information was provided during this tutorial. If not you can downloadthis full application by clicking the link provided below.

Note: for website developers the presentation layer can be your web page and youcan bind your data to the _Students or _classes collection objects as well.