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

IMPLEMENTING THE BUSINESS LOGIC

Chapter 21

489

 

 

 

 

lblMessage.Text=”Your account has been disabled. Please contact the network administrator.”;

return;

}

switch(Role)

{

case “Admin”: Response.Redirect(“.\\NA\\ManageUsers.aspx”); break;

case “BM”: Response.Redirect(“.\\BM\\AddFl.aspx”); break;

case “LOB”: Response.Redirect(“.\\LOB\\CreateRes.aspx”); break;

}

}

else

lblMessage.Text=”Incorrect password”;

}

dataSet11.Clear();

}

}

The Logoff.aspx Form

The Logoff.aspx form is used for logging a user off from the Web site. This form clears the Session variables assigned to the user so that the user is unable to browse any page on the Web application. All code on the Logoff.aspx form is written in the Load event of the form. To write the code for the Load event, double-click on the form in the Design view. The code for the Load event of the Logoff.aspx form is given as follows:

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

{

Session.RemoveAll();

}

490 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

Coding the Forms for Network

Administrators

SkyShark Airlines provides the ManageUsers.aspx and ManageDatabases.aspx forms for network administrators.

By default, when the application is installed, a user account for network administrators is added to the application,with the username and password as Admin and Password, respectively, so that a network administrator can access the ManageUsers.aspx form and create user accounts. You can examine the code for the ManageUsers.aspx form.

The ManageUsers.aspx Form

The ManageUsers.aspx page is used for adding and deleting user accounts. I will examine the steps to add and delete user accounts separately. However, before examining these tasks, perform the following steps to configure data controls for the ManageUsers.aspx form:

1.Drag the dtUsers table from Server Explorer to the Design view of the form.

2.Generate a dataset for the SqlDataAdapter control that is added to the form.

3.Modify the default queries that are associated with the sqlDataAdapter1 control as specified:

SelectCommand. SELECT Username FROM dtUsers

DeleteCommand. UPDATE dtUsers SET Role = ‘Disabled’ WHERE

(Username = @Original_Username)

Adding User Accounts

To add a user account, you need to perform the following steps:

1.Check whether the username already exists. Before adding a record to the dtUsers table, you should check whether the user account already exists. You can check usernames by retrieving them from the dtUsers table and checking each record. The following code snippet retrieves

IMPLEMENTING THE BUSINESS LOGIC

Chapter 21

491

 

 

 

 

records from the dtUsers table and compares them with the username specified by the user.

string username, password, role; int selection; role=lstAddRole.SelectedItem.Text; username=txtAddUserName.Text.Trim(); password=txtAddPassword.Text.Trim(); selection=lstAddRole.SelectedIndex; sqlConnection1.Open();

sqlDataAdapter1.Fill(dataSet11, “UserList”); sqlConnection1.Close();

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

{

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

{

lblMessage.Text=”The user name already exists. Please try another user name”;

return;

}

}

2.Add the new user to the database. If the username specified by the user is unique, the application adds a record to the database by using the SQL query associated with the InsertCommand property of the sqlDataAdapter1 control. However, before you execute the quer y, you need to assign values specified by the user as the parameters to the query. To assign values to parameters, you can use the Parameters collection of

InsertCommand. The code snippet to add a new user to the database is given as follows:

sqlDataAdapter1.InsertCommand.Parameters[0].Value=username; sqlDataAdapter1.InsertCommand.Parameters[1].Value=password; sqlDataAdapter1.InsertCommand.Parameters[2].Value=role;

sqlConnection1.Open();

sqlDataAdapter1.InsertCommand.ExecuteNonQuery();

sqlConnection1.Close();

492 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

NOTE

Instead of using the code in Step 2, you could also update the DataSet that corresponds to the data in the dtUsers table and then invoke the Update method of sqlDataAdapter1 to update data in the dtUsers table. The ideal scenario to employ that method is when you want to optimize database interaction by caching changes and then sending them to the database at regular intervals.

3.Send an e-mail message to the registered user. After adding the new user to the SkyShark Airlines application, use the SmtpMail class to send an e-mail message to the registered user. The Send method of the Smtp-

Mail class uses an object of the MailMessage class to send an e-mail message to the user. The code for creating and sending the message is given as follows:

MailAttachment attachment= new MailAttachment(“c:\\Inetpub\\wwwroot\\SkyShark\\NA\\PrivacyPolicy.doc”); MailMessage email= new MailMessage(); email.Attachments.Add(attachment);

email.To=username + “@skyshark.com”; email.From=”admin@skyshark.com”; email.Subject=”Message from SkyShark Airlines”;

email.Body=”Dear “ + username + “,\n\nYour account has been added “ + “to the SkyShark Airlines application. You can log on to the “ + “application at http://npandey-d185/skyshark. \n\nYour logon name” +

is “ + username + “ and the password is password. Please change” +

your password when you log on. \n\n By logging on to the application,” +

you agree to abide by the terms and conditions attached in the mail” + “\n\n Happy Browsing.\n\n Network Administrator (SkyShark)”; SmtpMail.Send(email);

When a new user registers on the Web site, the details of the new user are added to the dtUsers table and an e-mail message is sent to the user. The complete code

IMPLEMENTING THE BUSINESS LOGIC

Chapter 21

493

 

 

 

 

of the Click event of the Submit button is obtained by combining the code snippets given earlier. The code is given as follows:

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

{

if (txtAddUserName.Text==null || txtAddUserName.Text==”” || txtAddPassword.Text ==null || txtAddPassword.Text==”” || txtAddConfPassword.Text

==null || txtAddConfPassword.Text==””)

{

lblMessage.Text=”One or more required values are missing. Try again.”;

}

if (Page.IsValid)

{

string username, password, role; int selection; role=lstAddRole.SelectedItem.Text; username=txtAddUserName.Text.Trim(); password=txtAddPassword.Text.Trim(); selection=lstAddRole.SelectedIndex; sqlConnection1.Open();

sqlDataAdapter1.Fill(dataSet11, “UserList”); sqlConnection1.Close();

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

{

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

{

lblMessage.Text=”The user name already exists. Please try another user name”; return;

}

}

sqlDataAdapter1.InsertCommand.Parameters[0].Value=username; sqlDataAdapter1.InsertCommand.Parameters[1].Value=password; sqlDataAdapter1.InsertCommand.Parameters[2].Value=role; sqlConnection1.Open(); sqlDataAdapter1.InsertCommand.ExecuteNonQuery(); sqlConnection1.Close();

MailAttachment attachment= new MailAttachment(“c:\\Inetpub\\wwwroot\\SkyShark\\NA \\PrivacyPolicy.doc”);

494 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

MailMessage email= new MailMessage(); email.Attachments.Add(attachment); email.To=username + “@niit.com”; email.From=”nitinp@niit.com”; email.Subject=”Message from SkyShark Airlines”;

email.Body=”Dear “ + username + “,\n\nYour account has been added “ + “to the SkyShark Airlines application. You can log on to the “ + “application at http://npandey-d185/skyshark. \n\nYour logon name” +

is “ + username + “ and the password is password. Please change” +

your password when you log on. \n\n By logging on to the application,” +

you agree to abide by the terms and conditions attached in the mail” + “\n\n Happy Browsing.\n\n Network Administrator (SkyShark)”; SmtpMail.Send(email);

lblMessage.Text=”User added successfully”; txtAddUserName.Text=””;

dataSet11.Clear();

}

}

Deleting User Accounts

The procedure for deleting user accounts is straightforward. The username specified by the network administrator is checked in the dtUsers database to ensure that it exists. Next, the DeleteCommand property of the sqlDataAdapter1 control is used to delete the username specified by the network administrator from the database. The code for the Click event of the Delete button is given as follows:

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

{

string username=txtDelUserName.Text.Trim(); bool userexists=false;

if (username==null || username==””)

{

lblMessage.Text=”Please specify a valid user name”;

}

else

{

sqlConnection1.Open(); sqlDataAdapter1.Fill(dataSet11, “UserList”);