Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

IMPLEMENTING THE BUSINESS LOGIC

Chapter 21

495

 

 

 

 

sqlConnection1.Close();

foreach (DataRow myRow in dataSet11.Tables[“UserList”].Rows)

{

if (myRow[0].ToString().Trim().ToLower()==username.ToLower())

{

userexists=true;

}

}

if (userexists==false)

{

lblMessage.Text=”The user does not exist”; return;

}

sqlDataAdapter1.DeleteCommand.Parameters[0].Value=username; sqlConnection1.Open(); sqlDataAdapter1.DeleteCommand.ExecuteNonQuery(); sqlConnection1.Close();

lblMessage.Text=”User disabled successfully”; txtDelUserName.Text=””;

}

}

The ManageDatabases.aspx Form

The ManageDatabases.aspx form is used for moving data between the dtReser-

vations and dtDepartedFlights tables. It is also used to update the dtPassen-

gerDetails table for the frequent fliers program.

For updating the dtDepartedFlights and dtPassengerDetails tables,I have created stored procedures in SQL Server. These procedures are called from the SkyShark Airlines application so that the data can be updated directly at the back end. There are several advantages of using a stored procedure in this scenario:

Since the data is not required in the application, it does not need to be retrieved from the application and then posted back again.This saves a lot of unnecessary network congestion and improves the performance of the application and the database.

496 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

The developer does not need to write unnecessary code for the application. SQL queries that are used in stored procedures can be easily tested by using Query Analyzer.

To move data from the dtReservations table to the dtDepartedFlights table, you need to write the following stored procedure:

CREATE PROCEDURE UpdateReservations

@date datetime

AS

INSERT INTO dtDepartedFlights

SELECT * from dtReservations

WHERE (DateOfJourney < @date) AND (TicketConfirmed=1)

DELETE from dtReservations

WHERE (DateOfJourney < @date)

GO

To execute a stored procedure, you need to associate it with an SqlCommand object. Stored procedures can be associated with SqlCommand objects in the same way as you associate SQL Server tables with your application. The stored procedures in the SkyShark Airlines database are shown in Figure 21-2.

FIGURE 21-2 Stored procedures can be accessed from Server Explorer

IMPLEMENTING THE BUSINESS LOGIC

Chapter 21

497

 

 

 

 

To write the code for executing the UpdateReservations stored procedure from the SkyShark Airlines application,drag the Update Reservations stored procedure form Ser ver Explorer to the design view of the form. Visual Studio .NET automatically creates the sqlDataAdapter1 and sqlCommand1 controls. To run the stored procedure when a user clicks on the Archive button, write the following code for the Click event of the Archive button:

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

{

lblMessage.Text=””; sqlConnection1.Open();

sqlCommand1.Parameters[1].Value=DateTime.Today.Date.ToShortDateString(); sqlCommand1.ExecuteNonQuery();

sqlConnection1.Close(); lblMessage.Text=”Done.”;

}

To move data between the dtDepartedFlights and dtPassengerDetails tables, I

have created the FrequentFlier stored procedure.The definition of this procedure is given as follows:

CREATE PROCEDURE FrequentFlier

AS

DELETE dtFrequentFliers

INSERT INTO dtPassengerDetails

SELECT EMail, Sum(Fare), Count(EMail) from dtDepartedFlights where EMAIL!=’NotSpecified’ group by EMail

GO

To run this procedure, specify the following code in the Click event of the Update button:

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

{

lblMessage.Text=””; sqlConnection1.Open(); sqlCommand2.ExecuteNonQuery(); sqlConnection1.Close(); lblMessage.Text=”Done.”;

}

of the form:

498 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

The ChangePassword.aspx Form

The ChangePassword.aspx form is included in the folders for network administrators, business managers, and LOB (line-of-business) executives. However, I will discuss the coding and functionality of this form in this section only. The functionality remains same across the forms for all the roles.

To add functionality to the ChangePassword.aspx page, drag the dtUsers table from Server Explorer to Component Designer. In the resulting sqlDataAdapter1 control that is added to the form, change the UpdateCommand property as mentioned here:

 

 

Y

UPDATE dtUsers SET Password = @Password, PasswordChanged = ‘1’ WHERE (Username =

@Original_Username)

L

 

F

After specifying the preceding query, double-click on the Submit button to code

 

M

 

 

A

 

 

E

 

the functionality for its Click event. Write the following code for the Click event

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

{

sqlConnection1.Open(); sqlDataAdapter1.UpdateCommand.Parameters[0].Value=txtPassword.Text.Trim(); sqlDataAdapter1.UpdateCommand.Parameters[1].Value=Session[“usrName”]; sqlDataAdapter1.UpdateCommand.ExecuteNonQuery();

sqlConnection1.Close(); Response.Redirect(“ManageUsers.aspx”);

}

The preceding code accepts the new password specified by the user as the first parameter and the username, which is retrieved from the Session state variables, as the second parameter to update the password of the user in the dtUsers table.

Restricting Access to Web Forms

One aspect that is common across all Web pages of the application is that the users should be able to access Web forms pertaining to a role only if they are in that role. For example, the ManageUsers.aspx form should be accessible to network administrators only.

Team-Fly®

IMPLEMENTING THE BUSINESS LOGIC

Chapter 21

499

 

 

 

 

The SkyShark Airlines application enforces this constraint by using Session variables. The role of the user is queried from these variables in the Load event of all forms. When the role of the user matches with the intended audience of the form, the user is allowed to load the Web form. If the user should not be allowed to access the page, the user is redirected to the default.aspx page.The code that controls access to Web pages in the Load event of forms for network administrators is given as follows:

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

{

if (Session[“usrRole”]==null)

{

Response.Redirect(“..\\default.aspx”);

}

if (!(Session[“usrRole”].ToString()==”Admin”))

{

Response.Redirect(“..\\default.aspx”);

}

else

{

txtUser.Text=”Changing password for “+ Session[“usrName”].ToString();

}

}

NOTE

You need to add the code given here in the Load event of all forms. The only precaution you need to take is that you should change the value to check in the if clause ((!(Session[“usrRole”].ToString()==”Admin”))) to “BA” for business managers and “LOB” for LOB executives.