Handling Multiple Parameters in a Web Service Method

Just as the standard functions you create within your programs can support a number of parameters, the same is true for a web service. The SalesTax web services provides the CalcTax method that returns the sales tax on an amount (a value of type double) based on the state within which the user lives:

double GetTax(string State, double amount)

As you can see, the method supports two parameters, one of type double and one of type string. The method uses the tax rates found at www.taxadmin.org. To create the SalesTax 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 C# 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 SalesTax. 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 double GetTax(string State, double amount)       {          double rate;          switch (State)          {             case "AK":             case "DE":             case "MT":             case "NH":             case "OR": rate = 0; break;             case "CO": rate = 2.9; break;             case "AL":             case "GA":             case "HI":             case "LA":             case "NY":             case "SD":             case "WY": rate = 4.0; break;             case "MO": rate = 4.225; break;             case "NC":             case "OK":             case "VA": rate = 4.5; break;             case "UT": rate = 4.75; break;                        case "KS": rate = 4.9; break;             case "IN":             case "IA":             case "ID":             case "ME":             case "MD":             case "MA":             case "NE":             case "NM":             case "ND":             case "OH":             case "SC":             case "VT":             case "WI": rate = 5.0; break;             case "AR": rate = 5.125; break;             case "AZ": rate = 5.6; break;             case "CT":             case "FL":             case "KY":             case "MI":             case "NJ":             case "PA":             case "TN":             case "WV": rate = 6.0; break;             case "IL":             case "TX": rate = 6.25; break;             case "MN":             case "NV":             case "WA": rate = 6.5; break;             case "MS":             case "RI": rate = 7.0; break;             case "CA": rate = 7.25; break;             default: rate = -1.0; break;          }                    return(rate / 100.0 * amount);       }
  4. After you enter the code, select the Build menu Build Solution option to create the service.

The code uses a switch statement to determine the state specified by the calling program. After the code determines the state, it assigns the corresponding tax rate to the rate variable. Finally, the method returns the tax amount by multiplying the rate (which the code converts from a percentage to a decimal value by dividing the rate by 100.0) times the purchase amount.

Putting the SalesTax Web Service to Use

The following C# program, ShowTax.cs, displays a form within which you can enter a dollar amount. The form also contains a list from which you can select a state. After you enter the data and click the Calculate Sales Tax button, the program will use the SalesTax web service to determine the corresponding tax, which the program displays as shown in Figure 2.7.


Figure 2.7: Using the SalesTax web service within a C# program

To create the ShowTax.cs program, 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 Windows Application. Within the Name and Location fields type ShowTax. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls (label, buttons, and text box).

  3. Using the Toolbox, drag and drop the radio-button group, list box, button, and text box previously shown in Figure 2.7 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/SalesTax/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)       {         localhost.Service1 TaxObject = new localhost.Service1();         try           {              double TaxAmount = -1;                            if (listBox1.SelectedItem == null)                textBox1.Text = "Select a state";              else                {                  if (radioButton1.Checked)                    TaxAmount = Ä                  TaxObject.GetTax(listBox1.SelectedItem.ToString(), Ä                    20.00);                  else if (radioButton2.Checked) Ä                   TaxAmount = Ä                  TaxObject.GetTax(listBox1.SelectedItem.ToString(),                     50.00);                  else if (radioButton3.Checked) Ä                   TaxAmount = Ä                  TaxObject.GetTax(listBox1.SelectedItem.ToString(),                     100.00);                  else                    textBox1.Text = "Select a dollar amount";                }              if (TaxAmount != -1)                textBox1.Text = TaxAmount.ToString();           }         catch (Exception Ex)           {             textBox1.Text = "Exception: " + Ex.Message;           }       }

The program first validates the user input to ensure that the user has selected a purchase amount and a state. Then, the program creates an object the code will use to interact with the web service. The program uses the object to call the GetTax method, passing to the method the purchase amount and state abbreviation. The program 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