Creating the Web Service for Rent-a-Car XML

In this section, you will create the Web service, which a mobile application will consume. Before moving on to the actual creation of a Web service, we will discuss the database tables that the Web service will use.

Database of Rent-a-Car

The tables in the database of Rent-a-Car are discussed in the following list:

  • Reservation table. This table contains entries for all reservations that are made at Rent-a-Car. To store this information, the table contains fields, such as the pickup location as specified by a client who makes the reservation, the date and time of the pickup, the type of car the client wants, and so on. The type of car can include economy, compact, intermediate, premium, and luxury in the increasing order of the rental cost per day. Also, the client needs to specify the number of days for which the car is required. In addition, the name, the customer password, and the customer phone number are stored in the Reservation table. The customer uses the password to modify or delete the reservations that are made. The structure of the Reservation table is shown in Figure.
  • The Reservation Table.

    The Reservation Table.

  • VehicleTypes table. This table contains a list of the different vehicles available at Rent-a-Car and their rental cost per day. The structure of the VehicleTypes table is shown in Figure.
  • The VehicleTypes Table.

    The VehicleTypes Table.

  • To access data from these tables, the Web service uses stored procedures. These stored procedures are discussed in the following list:
  • GetRate stored procedure. This stored procedure accepts a vehicle type and returns the fare per day for it from the VehicleTypes table.
  • Reserve stored procedure. This stored procedure is used to enter a record into the Reservation table.
  • GetVType stored procedure. This stored procedure returns a list of all vehicle types.
  • UpdateReservation stored procedure. This stored procedure updates the reservation details for a particular customer.
  • ViewReservation stored procedure. This stored procedure views the reservation details for a particular customer.

Note?/td>You can use the SQL Server Enterprise Manager to create the stored procedures.

You will now write the code to create the preceding stored procedures. The code for the GetRate stored procedure is as follows:

Now add the code for the Reserve stored procedure as follows:

Then add the code for the GetvType stored procedure:

Note?/td>You can use the GetVType stored procedure when you want to populate a user-interface element, such as a list box with the values of all possible vehicle types. Next, add the code for the Update Reservation stored procedure:

Finally, add the code for the ViewReservation stored procedure. The code for the ViewReservation stored procedure is as follows:

Creating the ASP.NET Web Service

After the stored procedures are created, the next step in creating the Web service is creating an ASP.NET Web service by using Visual Studio .NET. To create an ASP.NET Web service, perform the following steps:

  1. Launch Visual Studio .NET from the Programs menu.
  2. Select the File, New, Project option.
  3. In the New Projects dialog box that is displayed, create a Visual Basic .NET project.
  4. Select the ASP.NET Web Service option in the Templates pane.
  5. Click on the OK button.

The preceding steps create a framework for a Web service application. Now you can modify the Web service code. In the Service1.asmx file for the Web service, add the declarations for various methods as shown in the following code:

The preceding code adds the declarations for the following methods:

  • MakeReservation() method. This Web method accepts the data the client enters to make a reservation and stores it in the Reservation table.
  • GetRate() method. This Web method calculates and returns the total fare for the selected vehicle type for a given number of days. To do this, the method accesses the rent per day from the VehicleType table and multiplies it by the number of days for which the customer wants to rent the car.
  • GetVehicleTypes() method. This Web method returns an array of strings that contain the different vehicle types applicable to the company.
  • CancelReservation() method. This Web method allows a client application to cancel a reservation after specifying the customer name and password.
  • ViewReservation() method. This Web method returns the details of the reservation that a customer made.
  • ModifyReservation() method. This Web method allows a client application to modify the details of a reservation that a customer made. Now you will learn to implement these Web methods.

Implementing the MakeReservation() Method

The MakeReservation() method accepts the data required to make a reservation. To do this, the Web method calls the Reserve stored procedure as shown in the following code snippet:

Note?/td>Please note that most of these Web methods access a database by using ADO.NET; therefore, you need to include the following statement in the file:

Implementing the GetRate() Method

The GetRate() method accepts a vehicle type, connects to the database, and retrieves the fare per day for the specified car type. Then the code multiplies the given fare with the number of days for which the reservation is made and returns the total fare. The code for the GetRate() method is shown in the following example:

The preceding code uses the ExecuteScalar() method of the SqlCommand object that returns the value in the first column of the first row of the result set.

Note?/td>In some of the previous chapters, we used the ExecuteReader() method to retrieve data from the database. The ExecuteReader() method returns the result as an object of the SqlDataReader class. However, if you need to retrieve only one value, as in this case, the ExecuteScalar() method is preferred.

Implementing the GetVehicleTypes() Method

The GetVehicleTypes() method returns an array of strings to the client application as shown in the following code:

Implementing the CancelReservation() Method

The CancelReservation() method accepts the name, key (password), and pickup date for a user and deletes the reservation by calling the CancelReservation stored procedure. The code for the CancelReservation() method is as shown:

Implementing the ViewReservation() Method

The ViewReservation() method returns the details of a reservation. To do this, the user needs to specify the customer name and the customer key. The code for the ViewReservation() method is as shown:

Implementing the ModifyReservation() Method

After a reservation is made, you can make changes to it by using the ModifyReservation() method. The code for the ModifyReservation() method is as shown:

Building the Web Service

The final step in the creation of the Web service is to build the project and deploy the Web service. To build the project, either run the application or start Internet Explorer and connect to http:// localhost /rental /servicel.asmx. A list of operations that the Web service supports is displayed, as shown in Figure.

The Operations that the Web Service Supports.

The Operations that the Web Service Supports.

Now click on the GetRate() method, and in the resulting screen, as shown in Figure, specify the vehicle type as Premium and click on the Invoke button. Verify that the Web service returns the correct value.

Invoking a Web Method.

Invoking a Web Method.

After you have verified that the Web service is working, you will create the mobile application that consumes this Web service.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

XML Topics