Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

C# ПІДРУЧНИКИ / c# / Premier Press - C# Professional Projects

.pdf
Скачиваний:
475
Добавлен:
12.02.2016
Размер:
14.7 Mб
Скачать

448 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

4. In the Data Link Properties dialog box, configure the connection to the database that you created in the previous section and click on OK. The data connection that you created will be displayed on the Choose Your Data Connection screen of the Data Adapter Configuration wizard.

5. Click on Next. The Choose Query Type screen will appear.

6.

On the Choose Quer y Type screen, retain the default option and click

 

on Next. The Generate the SQL Statements screen will appear.

7.

Specify the SQL Query as specified and click on Next.

 

 

 

 

F

 

Select UserName, Password from

ogon where (UserName=@username)

8.

 

 

 

M

On the View Wizard results screen,Yclick on Finish to complete the

 

wizard.

 

A

L

When you complete the wizard, new SqlDataAdapter and SqlConnection con-

 

 

E

 

trols are added to your project.These controls appear in Component Designer, as

 

 

T

 

 

shown in Figure 19-5.

 

 

 

FIGURE 19-5 Adding new SqlDataAdapter and SqlConnection controls to the form

In the preceding steps, you used the Data Adapter Configuration wizard to configure the SqlDataAdapter and SqlConnection controls. However, Visual Studio

.NET offers another simple mechanism to configure these controls without traversing the wizard. This method is specified below:

Team-Fly®

BASICS OF ASP.NET WEB APPLICATIONS

Chapter 19

449

 

 

 

 

1.Click on the View menu and select Server Explorer to open the Server Explorer window.

2.In the Server Explorer window, navigate to the table for which you want to configure the data adapter. For example, the path to the Logon table is shown in Figure 19-6.

3.Press and hold the mouse button on the name of the table and drag it to the form. Visual Studio .NET will automatically add the SqlDataAdapter and SqlConnection controls to your form.

FIGURE 19-6 Using the Server Explorer to add data cont rols

In the SkyShark Airlines project, I will use Server Explorer to configure the connections to database tables.

After adding data controls to the Web form, you need to add a DataSet control to the form. To add the DataSet control to the form, perform the following steps:

1.Click on the Data menu and then click on Generate Dataset. The Generate Dataset dialog box will appear, as shown in Figure 19-7.

2.Click on OK to configure a new DataSet control and add it to Component Designer.

450 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

FIGURE 19-7 Adding a new DataSet cont rol

You have added all the required controls to configure your application. In the last step, add the following code for the Click event of the Submit button:

private void btnSubmit_Click(object sender, System.EventArgs e)

{

sqlConnection1.Open(); sqlDataAdapter1.SelectCommand.Parameters[0].Value=txtUserName.Text.Trim(); sqlDataAdapter1.Fill(dataSet11, “UserDetails”);

if (dataSet11.Tables[“UserDetails”].Rows.Count==0)

{

lblMessage.Text=”Invalid user name”;

}

else

{

if (dataSet11.Tables[“UserDetails”].Rows[0][1].ToString().Trim()== txtPassword.Text.Trim())

lblMessage.Text=”Welcome “ + txtUserName.Text; else

lblMessage.Text=”Invalid password”;

}

sqlConnection1.Close()

}

BASICS OF ASP.NET WEB APPLICATIONS

Chapter 19

451

 

 

 

 

In the preceding code, the following sequence of tasks is performed:

1.The connection to the database is opened by the Open function.

2.The value specified by the user for the username is assigned to the first parameter of the SELECT query. The first parameter is @username.

3.The Fill method of the SqlDataAdapter class is used for executing the select query and adding the resultant data to the data set. The Fill command accepts two parameters, the name of the data set and the name of the DataTable in the data set in which the data should be stored.

4.If the number of rows returned by the select command is 0 as determined by the Count property of the Rows collection of a DataTable, an error message is displayed to the user.

5.If the number of rows returned is greater than 0, the password specified by the user is validated against the password retrieved from the database. The password retrieved from the database is stored in the second column of the first row of a DataSet table and can be accessed at the position Rows[0][1].

TIP

The first member of a collection has the index 0.Therefore, to access the second element, which is the password in this case, the index that needs to be used is 1.

6.If the password specified by the user matches the password retrieved from the database, a welcome message is displayed. If the password does not match, an error message is displayed.

After specifying the preceding code, click on Debug and then Start to run the application. The output of the application, which is generated after you specify a valid username and password, is shown in Figure 19-8.

452 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

FIGURE 19-8 Validating user credentials against a data source

Summary

ASP.NET is a server-side scripting language that allows you to create dynamic Web pages. The Web pages that you create in ASP.NET are known as Web forms. Web forms are processed on the server and can be coded in any .NETcompatible scripting language, such as Visual C# or Visual Basic .NET.

ASP.NET provides a number of server controls and validation controls that can be added to Web forms. Some commonly used server controls are Label,TextBox, Button, DropDownList, ListBox, Calendar, and RadioButtonList. Validation controls in ASP.NET help validate user input in a field before data is processed on the server.

To make applications communicate with the database, you can use the SQL Server .NET data provider. The three primary classes provided by this data

provider are SqlConnection, SqlDataAdapter, and DataSet. The SqlConnection class

is used for creating a connection to the database.The SqlDataAdapter class is used for updating records in the database, and the DataSet class is used to cache the data that is retrieved from the database.

Chapter 20

Designing

the Application

454 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

The last two chapters discussed the project case study and the basics of ASP.NET applications. In this chapter, you will learn how to create the user interface and the database schema of the SkyShark Airlines application. The design of the application is based on the project case study described in Chapter

18, “Project Case Study and Design.”

The database schema is usually the first component to be finalized for an application. Any changes in the database schema at a later stage in the development of your application can lead to tremendous developmental overheads. Therefore, I will first finalize the structure of the database and then proceed with the design of forms.

Creating the Database Schema

I discussed the structure of the database schema in detail in the section “Database Design” of Chapter 18. The schema is displayed in Figure 20-1.

FIGURE 20-1 Database schema for SkyShark Airlines

DESIGNING THE APPLICATION

Chapter 20

 

455

 

 

 

 

 

 

In this section, you will learn about the steps to create the database schema. You can use either SQL Server Enterprise Manager or Query Analyzer to create the database structure. If you choose SQL Server Enterprise Manager, you can use the MMC (Microsoft Management Console) based interface to graphically design your application. However, if you choose Query Analyzer, you need to specify SQL (structured query language) statements to design database tables and manage relationships.

In this section, I will examine how to create the database structure by using Query Analyzer.

Creating Database Tables

To use Query Analyzer for creating databases, you need to open Query Analyzer and connect to the SQL Server on which you want to create the database. The steps to open Query Analyzer and connect to a database are given as follows:

1.Click on Start. The Start menu will appear.

2.From the Programs menu, select Programs and then select Microsoft SQL Server.

3.From the submenu of Microsoft SQL Server, select Query Analyzer. The SQL Query Analyzer window will open.

4.In the SQL Query Analyzer window, the Connect to SQL Server dialog box appears by default. If it does not appear, select the Connect option from the File menu.

5.The Connect to SQL Server dialog box is shown in Figure 20-2. In this dialog box, select the name of the SQL Server from the SQL Server list and specify the username and password to log on to the database.

6.Click on OK to connect to the database.

456 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

FIGURE 20-2 The Connect to SQL Se rver dialog box

After connecting to the SQL Server, you need to create the SkyShark Airlines database before you can create the database tables. To create the SkyShark Airlines database, run the following SQL script in Query Analyzer:

CREATE DATABASE SkyShark

GO

USE DATABASE SkyShark

GO

In the preceding SQL statements, I have created a SkyShark database and have changed the database context to SkyShark. All tables that I create now will be created in the SkyShark database. I now begin creating tables in this database.

CAUTION

While executing all SQL statements that follow in this section, ensure that the current database is specified as SkyShark. If this is not the case, all database tables will be created in the Master database.

DESIGNING THE APPLICATION

Chapter 20

457

 

 

 

 

Creating the dtUsers Table

The dtUsers table is used to store the username, password, and role of all users having access to the SkyShark Airlines application. To create the dtUsers table, execute the SQL script given as follows:

CREATE TABLE [dbo].[dtUsers] (

[Username] [char] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [Password] [char] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [Role] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [PasswordChanged] [bit] NULL

) ON [PRIMARY] GO

In the preceding statements, the dtUsers table is created and the Username, Password, Role, and Password fields are added to the table.

Creating the dtFltDetails Table

The dtFltDetails table is used to store details of all flights by SkyShark Airlines. The script to generate the dtFltDetails table is given as follows:

CREATE TABLE [dbo].[dtFltDetails] (

[FltNo] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [Origin] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [Destination] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [Deptime] [datetime] NOT NULL ,

[Arrtime] [datetime] NOT NULL ,

[AircraftType] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [SeatsExec] [int] NOT NULL ,

[SeatsBn] [int] NOT NULL , [FareExec] [int] NOT NULL , [FareBn] [int] NOT NULL , [LaunchDate] [datetime] NOT NULL

) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO