Using a BO-Layer in a Webforms project

Topic updated 17-1-2007

This topic describes how to use a BO-Layer generated with the GenWise Nhibernate Templates inside a Visual Studio 2005 Webforms project.

Once you have successfully built and compiled the project with GenWise you have a BO-Layer DLL and a Visual Studio 2005 project file. To create a Webform application using the BO-Layer follow these steps:

1.Open Visual Studio.Net 2005.
2.Create a new Website (File, New, Website)
3.Attach a existing Project to the solution.
4.Search the project on the disk.
5.Add a new reference to your windows project.
6.Build the form, and bind the BO layer to it.

 

Graphically the steps are as following:

Step 1 and 2.:

ExamNHWebformCP

 

Step 3: Add an existing project to the solution. (The BO layer .csproj)

ExamNHWinformAEP

 

And add the .csproj. This will add the BO-layer to the tree.

ExamNHWinformBOA

 

Add the Northwind BO project to your project as a reference. (right mouse on the webproject, Select add Reference)

Now manually copy the hibernate.config from your Bolayer directory to your website directory. The connectstring is in the file and is needed in the root.

Now lets build a quick form with a region data grid. Build a form with a Gridview control on it: Add 'using Northwind;' to the source code in the namespaces, if northwind is your BOlayer name.

Add the following code to Load from method:

          RegionFactory _rFact = new RegionFactory();

          RegionCollection _regions = _rFact.GetAll();

          this.GridView1.DataSource = _regions;

          this.GridView1.Databind();

 

That's it. Run the form, it should look like this:

ExamNHWebformRB

 

The complete form code looks like this (without the .aspx code):

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 BOLayer;

 

public partial class _Default : System.Web.UI.Page

{

   protected void Page_Load(object sender, EventArgs e)

   {

       RegionFactory _rFact = new RegionFactory();

       RegionCollection _regions = _rFact.GetAll();

       this.GridView1.DataSource = _regions;

       this.GridView1.Databind();

   }

}

 

If you want to select specific columns the code will we different. But you can use the VS designers to do it.

Select the datagrid and call up the Gridview tasks.

ExamNHWebformDSC

 

Press OK, You will get a screen where you can select the object factory:

ExamNHWebformCDS

 

Choose the factory of the object you want to bind.

The choose Getall for the factory method to use:

ExamNHWebformCDS1

 

Press finish. you now configured the Object datasource. There is no more code needed in your form. The form has no code anymore. You can now edit all data columns by going to edit columns:

ExamNHWebformCDS2

Here you can delete columns you do not want to see, or change the items templates like headers etc.