Create A Twotier Web Form

You can abstract your data access from your Web page by putting the data access code into a component. This gives you separation between presentation and data. This separation is useful if you want to have the flexibility of changing the database or data access without having to rewrite your presentation code and HTML.

The first step to creating a Web Application that is split into two tiers is to create a Data Access Layer. To create a data access layer, start by creating a C# source file. In this source file, you add code related to connecting to your data source. See page 126 for detail on how to program with ADO.NET (.NET framework classes that are used for data access). After you have a connection to the database, you need to add functions to retrieve data from the data source. This can be done with SQL Select statements or stored procedures.

When you are done creating the component, you can compile it to the /bin directory as a library and use it on a Web page.

CREATE A DATA LAYER

CREATE A DATA LAYER

□ Open a new document in your text editor.

Create a private string variable for holding state.

-0 Initialize the private string variable when an instance of the class is created.

° Add a function that returns a string.

'—0 Add the Get and Set functions for the public string, which work with the private variable.

□ Open a new document in your text editor.

Create a new namespace.

Add an alias to the System, System.Data, and System.Data.SqlClient namespaces.

Create a private string variable for holding state.

-0 Initialize the private string variable when an instance of the class is created.

~Q Add a public string variable that has a method to read the value of its variable and a method to write to the private string variable.

° Add a function that returns a string.

'—0 Add the Get and Set functions for the public string, which work with the private variable.

CREATE CUSTOM COMPONENTS

You can create a function in the data layer that takes the title type as a parameter. The following code shows you how to do this. The connection string is used to construct the class.

Example:

namespace DataLayer_ex { using System; using System.Data; using System.Data.SqlClient; public class DataObject {

private string _stringConnection; public DataObject(String connStr){ _stringConnection = connStr;

public DataView GetTitlesForType(string stringTitleType) {

SqlConnection sqlconnectionPubs = new SqlConnection(_stringConnection); SqlDataAdapter sqldataadapterTitles = new SqlDataAdapter

("select title, notes, price from" " titles where type='"

+ stringTitleType + , sqlconnectionPubs);

DataSet datasetTitles = new DataSet(); sqldataadapterTitles.Fill(datasetTitles, "Titles"); return datasetTitles.Tables["Titles"].DefaultView;

C: \.Inetpub\.iAKijroot>

Create a function that returns a DataView.

Create a SqlConnection that uses the stringConnection property.

m C:\WIH NT \System32\crnd.exe_

Microsoft Windows 2DDD [Version 5.DO.2195] (C) Copyright 1985 2DD0 Microsoft Corp.

C:\>cd C:\InetPubWwroot

C:\Inetpub\uuwroot>csc /t:library /out:binMJataObjeet.d11 DataObject.cs Microsoft CR) Visual Ctt Compiler Version 7.00.?1«i8 [CLR version w1.0.2615]

Copyright CO Microsoft Corp 2000. All rights reserved.

C: \.Inetpub\.iAKijroot>

Create a SQLDataAdapter variable that uses the connection to get all business titles.

Create a DataSet and fill with the data retrieved with the SQLDataAdapter.

Return the default view from the DataSet.

£ Go to the command prompt.

£ Compile the component using the csc command.

CONTINUED

The skill set for a developer that creates presentation layer code and layout is different than for a developer who writes data access code. Two-tiered Web applications allow for your Web application code to be in separate files, which makes it beneficial if you want to divide work based on developers' skill sets. Having separate files yields fewer problems with source control and allows for parallel development efforts. That is, two people can work on the same part of the site together by having separate files for presentation and data.

The Web Forms you create in ASP.NET for accessing data access components need to import the namespace for the data access component. This is done using the ©Import directive at the top of the page. Next, you can place a Server form containing controls on the page to display the data. One way to display data is using a DataList control. See page 130 for more information on working with the DataList control. After you have specified what to show in the DataList, you can create the code to populate the control. You can use the Page_Load function to do this.

CREATE A TWO-TIER WEB FORM

CREATE A TWO-TIER WEB FORM

E Open the

GenericTemplate.aspx template from the Code Templates directory.

§ Import the DataLayer namespace.

le Add a server form to the page.

Add a DataList to the page to display the results.

Add an ItemTemplate to the page to describe the output for each item in the result set.

E Open the

GenericTemplate.aspx template from the Code Templates directory.

§ Import the DataLayer namespace.

le Add a server form to the page.

Add a DataList to the page to display the results.

Add an ItemTemplate to the page to describe the output for each item in the result set.

CREATE CUSTOM COMPONENTS

You can create a Web page that retrieves different types of titles based on user input by putting the following code into a server-side script block. You need to compile

DataObject_ai.cs with DataObject_ai.bat to have this sample run. To see the full source code for this example, see TwoTierWebForm_ai.aspx on the CD-ROM.

http://localhosti'T woTierWebForm.aspx - Miciosoft Internet Explorer

File Edit View Favorites Tools Help

Add the Page_Load function to the page.

Add the Page_Load function to the page.

Create a new instance of the DataObject class and set the connection string.

http://localhosti'T woTierWebForm.aspx - Miciosoft Internet Explorer

File Edit View Favorites Tools Help

E3 Set the DataSource of the DataList on the Web page to the function in the data access Layer that returns the business titles.

LE Bind the DataList.

■T3 Back • ' J J {3 [T Personal Bar Q Search _*] Favorites (J -®V J sj © Jt s |2] http://lccalhostyTiweTierWebFcrm.aipx

Welcome to mylifetimegoals.com

Here are some of the resources to help you reach your business goals,. The Busy Executive's Database Guide

Art overview of available database systems with emphasis on common business applications. Illustrated.

19,99

Cooking with Computers: Surreptitious Balance Sheets

Helpful hints on how tu use your electronic resources to the best advantage.

11,95

You Can Combat Computer Stress!

The latest medical and psychological techniques fur living with the electronic office. Easy-to-understand explanations ? nn_

$4 Local intranet

" Save the file and request it from the Web server.

■ The title, notes, and price appear for the business titles from the pubs database using the data layer.

Was this article helpful?

0 0

Post a comment