Returning a Non-String Value from a Web Service

So far, the web services this chapter has presented have returned only string results. Within your programs, you will likely make extensive use of functions that return values of type integer, double, Boolean, and so on. The following DetermineAge web service returns a user’s current age in years (an integer value), based on the day, month, and year the user was born:

integer GetAge(integer Month, integer Day, integer Year)

To create the DetermineAge web service, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name DetermineAge. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the following program statements:

        <WebMethod()> Public Function GetAge(ByVal Month As Integer, _ Ä   ByVal Day As Integer, ByVal Year As Integer) As Integer         Dim Birthday As New DateTime(Year, Month, Day)         If (Year < 1900) Then           GetAge = -1         ElseIf (Year > Now.Year) Then           GetAge = -1         ElseIf (Month < 1) Or (Month > 12) Then           GetAge = -1         ElseIf (Day < 1) Or (Day > 31) Then           GetAge = -1         Else           GetAge = New DateTime(Now.Ticks - Birthday.Ticks).Year - 1         End If     End Function

  4. After you enter the code, select the Build menu Build Solution option to create the service.

The GetAge method does some simple error checking to validate the date the user entered. A better service would make sure the user specified a valid number of days for a specific month. If a program passes an invalid date, the method will return the value -1. The method

converts the user’s data into a DateTime structure. Then, the method subtracts the user’s date from the current system date, returning the year’s portion of the result, minus one. For example, if the user’s date is 12-01-1980 and the current date is 1-1-2003, the method would return 23-1 or 22 years old.

Putting the DetermineAge Web Service to Use

The following ASP.NET page, ShowMyAge.aspx, displays a form that prompts the user to enter the month, day, and year of his or her birth, as shown in Figure 2.6. After the user enters the data and clicks the submit button, the page calls the DetermineAge web service to determine the user’s age in years.

click to expand
Figure 2.6: Using the DetermineAge web service to display a user’s age

To create the ShowMyAge.aspx ASP.NET page, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click C# Projects. Then, within the Templates field, click ASP.NET Web Application. Finally, within the Location field, specify the folder within which you want to store the program and the program name ShowMyAge. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the program’s controls (label, buttons, and text box).

  3. Using the Toolbox, drag and drop the button and text boxes previously shown in Figure 2.6 onto the page.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type localhost/DetermineAge/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the following program statements:

          private void Button1_Click(object sender, System.EventArgs e)       {         if (TextBox1.Text.Length == 0)           TextBox4.Text = "Enter a month in the range 1 to 12";         else if (TextBox2.Text.Length == 0)           TextBox4.Text = "Enter a day in the range 1 to 31";         else if (TextBox3.Text.Length == 0)           TextBox4.Text = "Enter a year using 4 digits";         else           try             {            int Month, Day, Year, Age;                Month = Convert.ToInt32(TextBox1.Text);               Day = Convert.ToInt32(TextBox2.Text);               Year = Convert.ToInt32(TextBox3.Text);               localhost.Service1 WSObject = new localhost.Service1();               Age = WSObject.GetAge(Month, Day, Year);               TextBox4.Text = "Your age is: " + Age.ToString();             }           catch (Exception ex)             {               TextBox4.Text = ex.Message;             }       }

Again, the button-click handler performs some simple data-input validation and then creates an object the program will use to interact with the web service. The program calls the GetAge method, passing to the method the month, day, and year entered by user. The code assigns the method’s result to the text box.




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net