Click here to Skip to main content
12,998,863 members (70,183 online)
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 6 May 2002

N-Tier application using Managed C++ and ASP.NET - A Tutorial

, 6 May 2002
Rate this:
Please Sign up or sign in to vote.
A tutorial on how to set up an n-tier application using .Net
<!-- Download Links --> <!-- Add the rest of your HTML here -->


This tutorial provides a step-by-step walkthrough on how to build an n-tier web application using Visual Studio .Net and more specifically using Visual C++ .Net. The tutorial shows how to create a web service that communicates with a SQL Server database and how to create a simple grid for the client to view the contents of the database. I found very little documentation for using VC++ for building .Net applications so hope this is useful.

A Note about the Download

The zip file contains the solution and the VC++ project. In a sub folder called N_Tier_web_Client you will find the ASP .Net files. These need to be in a virtual directory in your c:\inetpub\wwwroot folder. I hope I have included all files. First time dealing with Visual Studio .Net. Given how easy it is to build a simple system as in my sample it may make more sense to create a new project rather than download and install the code.

Web Service

We will start by setting up a web service. There are other articles that explain how to create one so I will be brief. In the Microsoft development Environment select New Project. Then Select Visual C++ Projects on the left and for a template select “Managed C++ Web Service”. Give your project a name. I used N_Tier_Web_Service. Click OK and your project template is ready to use.

The template creates N_Tier_Web_Service.cpp, N_Tier_Web_Service.h, and N_Tier_Web_Service.asmx amongst other files. The template creates a sample class called class1 for you. I will just use this class. To this class lets add a web method to get us data from our database. In the N_Tier_Web_Service.h file lets add a method to class1 as follows:

// N_Tier_Web_Service.h

#pragma once

#using <System.Web.Services.dll>
#using <System.Data.dll>	//Added to use SqlClient classes
#using <System.Xml.dll>		//Needed for DataSet class

using namespace System;
using namespace System::Web;
using namespace System::Web::Services;
using namespace System::Xml::Serialization;
//using namespace System::ComponentModel;
using namespace System::Data;
using namespace System::Data::OleDb;
using namespace System::Data::SqlClient;

namespace N_Tier_Web_Service {
    public __gc 
        class Class1 : public WebService {
	// The HelloWorld() example service returns the string "Hello, World!".
	// To test this web service, ensure that the .asmx file in the deployment 
	// path is set as your Debug HTTP URL, in project properties.
	// and press F5.

        String __gc* HelloWorld();
	  DataSet __gc* GetDataFromDB();
	  DataSet __gc* GetDataFromOleDb();
        // TODO: Add the methods of your Web Service here

Please note that I have added #using System.Data.dll and #using System.Xml.dll. Also have added using namespace System::Data; and using namespace System::Data::SqlClient.

In the N_Tier_Web_Service.cpp file we define the function GetDataFromDB as:

DataSet __gc* Class1:: GetDataFromDB () {
	SqlConnection* myConnection = new SqlConnection( S"User 
ID=qwe;password=*******;Initial Catalog=planner;
Data Source=BOV;" );
// open connection
	SqlDataAdapter* myAdapter = new SqlDataAdapter( "SELECT * FROM tb", 
myConnection );
	DataSet* myDataSet = new DataSet();

// Fill DataSet using query defined previously for DataAdapter
	myAdapter->Fill( myDataSet, "Baby Activity" );
	return myDataSet;


DataSet __gc* Class1::GetDataFromOleDb() {
	OleDbConnection* myOleDbConnection = new OleDbConnection(
	S"User ID=Admin;Data Source=X:\\planner.mdb;Mode=Share Deny None;"
	S"Extended Properties=;Jet OLEDB:System database=;"
	S"Jet OLEDB:Registry Path=;Jet OLEDB:Database Password=;"
	S"Jet OLEDB:Engine Type=4;Jet OLEDB:Database Locking Mode=0;"
	S"Jet OLEDB:Global Partial Bulk Ops=2;"
	S"Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password=;"
	S"Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;"
	S"Jet OLEDB:Don't Copy Locale on Compact=False;"
	S"Jet OLEDB:Compact Without Replica Repair=False;"
	S"Jet OLEDB:SFP=False" );

// open connection
	OleDbDataAdapter* myOleDbAdapter
                = new OleDbDataAdapter( S"SELECT * FROM tb", myOleDbConnection );
	DataSet* myDataSet = new DataSet();
// Fill DataSet using query defined previously for DataAdapter
	myOleDbAdapter->Fill( myDataSet, "Baby Activity" );
	return myDataSet;

(Please note: I am certain the connect string for the OleDbConnection can be shorter. I copied it from a data connection I had created.)

You will see that I have implemented one function using the SqlClient classes and one using OleDb. SQLClient classes are designed for use with SQL Server whereas OleDb can be used with any database as long as you have built a provider or someone else has. The process of creating a connection and executing a query is simple. .Net introduces a new class called DataSet which will store the information as XML. I need to read up more about DataSet and see what all it can do for me, but at first glance it looks pretty cool. In the past I used to query databases and then convert the data into an XML format that I had come up with. And then had to write code to parse the data on the client prior to displaying it or putting it to some other use. With the DataSet class .Net has automated this process and made it very easy to use. You will see more of this when I set up a web client to display the data from the above query.

Now you can build and test the web service. If it works then you will get a whole bunch of XML with the data from your query. There are various articles on CodeProject that will show you how to build and test a .Net web service. When you build the project Visual Studio will copy the DLL and some other files to the Inetpub\wwwroot\ N_Tier_Web_Service folder. Your web service is ready to run.

Implement Integrated Security

The Microsoft documentation says that we need to enable and integrated security and this is done by: 1. In the Internet Services Manager locate the virtual directory for the web service (later for the client web pages too) and open properties by right clicking on the directory and selecting properties. Select the “Directory Security” tab. Disable anonymous access and enable integrated security. 2. In Solution Explorer (right side of your visual studio development environment where you can see your projects, files, classes etc.) Open web.config and add the line <identity impersonate = "true"> after <system.web>

I am not sure what step 2 does. .Net security is something I am still trying to learn about. And step one stopped my project from working. Once I created the web client it would not work till I reset security to anonymous access. I will have to learn up more about the security issues of IIS and .Net.

The Web Client

Now we will build an ASP .Net web client. We start by adding a new project to our solution. You can do this by right clicking on your solution, then select Add, then select New Project.

In the Add New Project wizard select Visual Basic Projects and then select ASP .Net Web Application. Type a name for your project. Be careful not to type a lengthy name as I did. This name will also be the name for your web site. When you click ok, the development environment will present you with a designer screen to create a web form. We will use this default screen to place a grid to show the data queried from the database.

But before we do all that. First we have to add a web reference to the web service we created. This is also where we see how neat .Net is. Now I have not tried this but basically you can add a web reference to any web service on any web server. To create a web reference right click on the name of your project in Solutions Explorer and then select “Add Web Reference”.

When presented with the Add Web Reference form you have to type in the URL of your web service. Let the wizard find your web service then click on “Add Reference.” The “Add Web Reference” dialog will also show you the classes and interfaces provided by the web service.

Now we will add a DataSet to the ASP form. To do this simply select Data then DataSet from the toolbox. The “Add Dataset” dialog will ask you to select a typed DataSet or an untyped DataSet. Typed Datasets are those that have already been created. If your web service had defined a DataSet you could select it. If you created a web service using ASP .Net you get to drag and drop a DataSet that the web service will provide. Using VC++ for creating a web service does not offer this. Or at least I have yet to find it. But this is not an issue. Simply choose Untyped DataSet.

Now click on Toolbox, WebForms, DataGrid. Drag and drop the DataGrid on your form. Right click on the DataGrid and edit its proerties and set DataSource to be DataSet1 (assuming you did not give your DataSet another name). The DataGrid has some neat functionality too but I am not going into those details.

Remember to implement Integrated Security for the Client project too the same way you did for the web service.

Now right click on the WebForm and select view code and in the Page_Load function put the following code.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e _
                            As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim webService As New N_Tier_Web_Client.localhost.Class1()

        If Not Page.IsPostBack Then
        End If

End Sub

Build and test your solution. You now have an n-tier solution with a SQLServer backend, VC++ .Net web service, and an ASP .Net client. Using the exact same steps you can add another web form and then drop a DataSet and DataGrid into it but in this case call the GetDataFromOleDb() function. In my example the GetDataFromOleDb actually runs a query to a MS Access database. So essentially we have a web service that talks to both SQLServer and Access. Not sure why anyone would want that but I was simply testing out the various classes.

You can see that building an n-tier solution is extremely simple using the .Net technology. Even though there is almost no documentation on how to do almost anything with VC++ .Net, one can sort of figure it out. And once you get it you will realize that it’s quite simple.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Ranjan Banerji
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Generaldifficult to read Pin
Thomas Freudenberg6-May-02 23:17
memberThomas Freudenberg6-May-02 23:17 
GeneralRe: difficult to read Pin
Ranjan Banerji6-May-02 23:27
memberRanjan Banerji6-May-02 23:27 
GeneralRe: difficult to read Pin
Thomas Freudenberg6-May-02 23:40
memberThomas Freudenberg6-May-02 23:40 
GeneralRe: difficult to read Pin
Ranjan Banerji7-May-02 0:18
memberRanjan Banerji7-May-02 0:18 
GeneralRe: difficult to read Pin
Thomas Freudenberg7-May-02 0:22
memberThomas Freudenberg7-May-02 0:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170622.1 | Last Updated 7 May 2002
Article Copyright 2002 by Ranjan Banerji
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid