WorldofASP.NET : ASP.NET Directory, Tutorial, Hosting, and Source Code
You are 1 of 119 users


WorldofASP.NET >> ASP.NET >> Database

Using Codesmith and .netTiers to build a Data Access Layer

Tutorial about how to connect database with asp.net and vb.net using Codesmith and .netTiers tools
Published Date : 24 Mar 2008
Author : Hans Candra
Language : VB.NET,VBScript
Platform : Wins,.NET
Technology : Visual Studio,ASP.NET,IIS,Win Forms
Views : 15840
Rating : (1 votes so far)



Introduction

Some time ago, I have involved in a National Energy company information system (CIS) development. It is actually the project developed by my company and I am a junior of the developer team. We start it by explaining and discussing about all the detail and requirement of database because this is the main part and most important part of this project. My data layer syntax in asp.net is using codesmith and .nettiers as tools to simplified and shortened my programming.

Main

Codesmith is a client application that uses templates to generate code. It saves time. It reduces repetitive coding. .NetTiers is an open source tools that provides templates for codesmith to generate a n-tiered architecture. Combine these 2 client applications can quickly create the datalayer for the project. It does simply provide a Business Object like Employees, Companies, and others. This is done by creating collections. You can insert, update, select from databases in a different way besides ado.net method as usual. Maybe you can view the details of what these two are from their sites.

If you want to try, first you need to download codesmith and .nettiers and then install them, mine is using codesmith 4.0. Then open codesmith explorer. Click open folder from top corner of codesmith explorer then browse to your nettiers installation folder. After that execute Nettier.cst as shown below. 





So there are minimum three attribute now you must config in the next opened window. It is select database source you want to use in the project development. In this case mine is CISdata which have been designed before using Power Designer 11. Second set the output directory. And third is set the namespace. Namespace is used for your database collection access later. For example my namespace is CISLib. Later I can write Imports CISLib, Imports CISLib.DAL, Imports CISLib.DAL.DataRepository, Imports CISLib.DAL.SqlClient in my asp.net vb coding. Last click generate. 






Now we can wait for quite some time depends on your computer specification. It takes about 1-2 minutes to complete. If all goes well, you’ll have a new solution in the folder you selected as output directory. Open the solution. You’ll see that it has three projects. All three set to produce DLLs. Next most important step is to copy all .DLL files you generated from the output bin directory to your project bin directory. 





Last thing is to config Enterprise Library Configuration from nettiers. Navigate to Start Menu, nettiers, Enterprise Library Configuration. Minimize it. Open Your visual studio solution. Add another project to the solution. Add a new item to the project. Select Application Configuration File as the type. It’s called App.conf by default. Leave it as it is. Open Enterprise Library Configuration window. Click on Open File and select the App.Conf you just created.

Three important part to config is nettiers application block, SQL data provider instance, SQL connection string->database, integrated security, and server. After all save it. This generates some *.conf files in your project directory. Go there and copy all of the *.conf files to your bin directory. This is the end of codesmith and nettiers installation and configuration part. Next step is how to use it. 

 


I’ll include part of my asp.net vb coding to let us understand how to use this as a collection to connect to the database which we already generated before. 

Imported library.

 

The method to insert to the database.

Key syntax here are 'Dim objCompany as New COMPANIES'. Table COMPANIES in database now can be inserted with values from input textbox on asp.net form with easier method, example insert value to COMPANY_ID, STATUS_ID filed in COMPANIES table is just as simple as shown below, instead of declare connection, command, and executenonquery as usual.

 

The method to select/retrieve data from the database.

Key Syntax is also in declaration of COMPANIESCollection object. We can notice here object declare as COMPANIESCollection for database retrieve instead of only as COMPANIES for database insert. One more important syntax is COMPANIESProvider.getpaged. It is the syntax used in codesmith and nettiers implementation. There are a lot more of syntax we can use for this data layer connection. For more expertise explanation about it, maybe we can look from their original help and tutorial.

Region "Company"

    Private Sub showCompanyData()

        Dim objCompanyCol As New COMPANIESCollection

        Try

            objCompanyCol = COMPANIESProvider.GetPaged("COMPANY_ID='2'", "", 0, 999, 999)

 

            If objCompanyCol.Count > 0 Then

                txtCompanyID.Text = objCompanyCol(0).COMPANY_ID

            End If

        Catch ex As Exception

            Response.Write(ex.ToString())

        End Try

    End Sub

 

    Private Sub getCompanyIDMaxnum()

        Dim objCompanyCol As New COMPANIESCollection

        Try

            objCompanyCol = COMPANIESProvider.GetPaged("", "CAST(COMPANY_ID AS INT) DESC", 0, 999, 999)

 

            If objCompanyCol.Count > 0 Then

                txtCompanyID.Text = CStr(CInt(objCompanyCol(0).COMPANY_ID) + 1)

            Else

                txtCompanyID.Text = "1"

            End If

        Catch ex As Exception

            Response.Write(ex.Message)

        End Try

    End Sub

#End Region



Conclusion

The possibilities for CodeSmith templates are limited only by your own creativity. As I've demonstrated in this article, CodeSmith is an exceptionally powerful developer productivity tool. Coupled with the .NetTiers templates you can write a best practices Data Access Layer for your application in a matter of minutes.

References

All the useful links or references that can help you learn about this tutorial

  1. http://www.codesmithtools.com/
  2. http://www.nettier.com

                    Tag Cloud
                      "javascript in asp.net"   asp.net encryption   xhtml-mp asp.net   httpwebrequest httpwebresponse   httpwebrequest in asp.net 2.0   asp.net random number   httpwebrequest vb.net   asp.net url parameters   formview displaying inserted data   asp.net listbox control   httpwebrequest .net   asp.net delegate   asp.net pass data from one page to another   httpwebresponse   asp.net cookie shopping cart   ajax updatepanel button   frames image gallery thumbnails asp.net   asp.net tooltip   asp.net file upload progress bar   edit update insert in a gridview c#   asp.net upload ajax progress   httpmodule httphandler   generate random number asp.net   asp.net activate user   smart device application   httpwebrequest asp.net c#   tooltip asp.net   feedback form in asp.net   cannot start service from the command line or a debugger. a windows service must   upload file progress bar asp.net   encrypt querystring in asp.net   contact us asp.net   formview asp.net   httpwebrequest   asp.net cookies encryption   form view in asp.net   asp.net file upload progress   asp httpwebrequest   checkboxlist datasource   httpwebrequest create   cannot start service from the command line or a debugger   create httpwebrequest   asp.net httpwebrequest   datalist control





                    Other Related and Popular Articles :

                    Using Stored Procedures with MSSQL and ASP.NET 2.0
                    This article explain how to migrate your existing in line SQL to Stored Procedures with MSSQL and ASP.NET 2.0

                    Basic Introduction to ADO.NET in ASP.NET
                    This article provides information on programming with ADO.NET including DataSet,DataAdapter and DataReader

                    Improve Your DataGrid Paging performance
                    Tips and tricks on how to improve your DataGrid Paging performance


                    Author Profile : Hans Candra

                    Click here to view Author Profile


                    How would you rate the quality of this content?
                    Poor Excellent

                    Comments

                    Leave New Comments


                    Article Content copyright by Hans Candra
                    Everything else Copyright © by WorldofASP.NET 2008

                    Category
                    .NET 3.5
                    AJAX and ATLAS
                    ASP.NET
                    C# Programming
                    Classic ASP
                    Enterprise Systems
                    General .NET
                    VB.NET Programming
                    Announcements
                    Earn Cash by writing an article or review
                    For more info Click here







                    Legend : - Within 3 Days - Within 6 Days - Within 9 Days

                    Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 | Link To Us | Contact Us
                    © 2002-2008 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
                    Our Partners : ASP.NET Web Hosting | Windows Web Hosting | ASP.NET Hosting | Phone Card | PHP Directory | Bangkok Hotels |Calling Card