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

500 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

Coding the Forms for

Business Managers

Business managers use the AddFl.aspx, RequestID.aspx, Reports.aspx, and FreqFl.aspx forms for their business operations. In this section, you can learn to add functionality to these forms.

The AddFl.aspx Form

The AddFl.aspx form is used for adding details of new flights. To add data to the AddFl.aspx table, configure SqlConnection and SqlDataAdapter controls by dragging the dtFltDetails table to the form. Next, change the SelectCommand and InsertCommand properties of the sqlDataAdapter1 control as mentioned here:

SelectCommand= SELECT FltNo FROM dtFltDetails

InsertCommand= INSERT INTO dtFltDetails (FltNo, Origin,

Destination, Deptime, Arrtime, AircraftType, SeatsExec, SeatsBn, FareExec, FareBn, LaunchDate) VALUES (@FltNo, @Origin, @Destination, @Deptime, @Arrtime, @AircraftType, @SeatsExec, @SeatsBn, @FareExec, @FareBn, @LaunchDate)

The steps to add new flights to the airline are as follows:

1.Ensure that the flight number is unique. The flight number specified by the business manager is queried in the dtFltDetails table. If the flight number is not unique, an error message is displayed to the user. You need to write the following code to ensure that the flight number is unique:

dataSet11.Clear(); sqlConnection1.Open(); sqlDataAdapter1.Fill(dataSet11,”FltNos”); sqlConnection1.Close();

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

{

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

IMPLEMENTING THE BUSINESS LOGIC

Chapter 21

501

 

 

 

 

{

lblMessage.Text=”The flight already exists. Please try another

flight number.”;

return;

}

}

2.Validate the departure and arrival times. The departure and arrival times for flights need to be specified in the correct format. The departure and arrival times specified by the user are converted into date and time format by using the ToDateTime function of the Convert class. The resultant values are stored in TimeSpan structures. The code to validate departure and arrival times is given as follows:

TimeSpan deptime, arrtime; try

{

deptime=Convert.ToDateTime(txtDepTime.Text).TimeOfDay; arrtime=Convert.ToDateTime(txtDepTime.Text).TimeOfDay;

}

catch

{

lblMessage.Text=”Invalid departure or arrival time”; return;

}

3.Update flight details. The details of the new flight are added as parameters to the InsertCommand query. Next, you need to open the connection to the database and execute the query. Then you can close the connection to the database and clear all fields of the AddFl.aspx form. The code snippet to update flight details is given as follows:

try

{

sqlDataAdapter1.InsertCommand.Parameters[0].Value=txtFltNo.Text.Trim(); sqlDataAdapter1.InsertCommand.Parameters[1].Value=txtOrigin.Text.Trim(); sqlDataAdapter1.InsertCommand.Parameters[2].Value=txtDestination.

Text.Trim();

sqlDataAdapter1.InsertCommand.Parameters[3].Value=deptime.ToString(); sqlDataAdapter1.InsertCommand.Parameters[4].Value=arrtime.ToString();

502 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

sqlDataAdapter1.InsertCommand.Parameters[5].Value=txtAircraft. Text.Trim();

sqlDataAdapter1.InsertCommand.Parameters[6].Value= Convert.ToInt32 (txtSeatsExec.Text.Trim());

sqlDataAdapter1.InsertCommand.Parameters[7].Value= Convert.ToInt32 (txtSeatsBus.Text.Trim());

sqlDataAdapter1.InsertCommand.Parameters[8].Value= Convert.ToInt32 (txtFareExec.Text.Trim());

sqlDataAdapter1.InsertCommand.Parameters[9].Value= Convert.ToInt32 (txtFareBn.Text.Trim());

sqlDataAdapter1.InsertCommand.Parameters[10].Value= DateTime.Today.Date. ToShortDateString();

sqlConnection1.Open(); sqlDataAdapter1.InsertCommand.ExecuteNonQuery();

}

catch

{

lblMessage.Text=”Unable to add flight details.”; sqlConnection1.Close();

return;

}

sqlConnection1.Close(); lblMessage.Text=”Flight added successfully.”; txtFareBn.Text=””;

txtFareExec.Text=””; txtSeatsBus.Text=””; txtSeatsExec.Text=””; txtArrTime.Text=””; txtDepTime.Text=””; txtDestination.Text=””; txtOrigin.Text=””; txtFltNo.Text=””; txtAircraft.Text=””;

}

The complete code for the Submit button of the form is obtained by combining the three preceding code snippets.

IMPLEMENTING THE BUSINESS LOGIC

Chapter 21

503

 

 

 

 

The form also includes a Cancel button that is used to clear the values of all fields. The code for the Click event of the Cancel button is given as follows:

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

{

txtFareBn.Text=””; txtFareExec.Text=””; txtSeatsBus.Text=””; txtSeatsExec.Text=””; txtArrTime.Text=””; txtDepTime.Text=””; txtDestination.Text=””; txtOrigin.Text=””; txtFltNo.Text=””; txtAircraft.Text=””;

}

The RequestID.aspx Form

The RequestID.aspx form is used for making a request for a new user account. The business manager specifies the username and the role of the new user and submits the request to the network administrator by e-mail. The code for the Click event of the Submit button is given as follows:

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

{

string to, from, subject, body; to=”admin@skyshark.com”; from=Session[“usrName”].ToString() + “@niit.com”;

subject=”New User Request”;

body=”I would like to request for a new user. The details are given below:\n\n”+

“User Name: “ + txtUserID.Text + “\n\nRole: “ + lstRole.SelectedItem.Text + “\n\nThanks!\n\n” + Session[“usrName”].ToString();

SmtpMail.Send(from, to, subject, body); txtUserID.Text=””; lstRole.SelectedIndex=0;

lblMessage.Text=”Request sent successfully”;

}