Coding the Web service

After you add the data controls to the Web service, you need to code the methods of the Web service. This chapter demonstrates how you can code a method to add products to the Products database by using the Web service.

Before you code the methods of the Web service, add a description to and change the default namespace of the Web service. The description and the namespace of the Web service enable a Web service client developer to understand the usage of the Web service. To add a description and change the default namespace associated with the Web service, follow these steps:

1. Double-click the Component Designer to open the Code Editor.

2. In the Code Editor, locate the statement public class Service1.

3. Add the following code before the statement that you located in Step 2:

4. [WebService(Namespace="",

5. Description="Use the Web service to add products to the

After you enter the preceding line of code, the namespace of the Web service is, and a description is added to the Web service.

Next, write the code for adding products to the Products table. This code needs to be written immediately below the declaration of the Web service. The code for the AddProduct method, which adds product details to the Products table, is as follows:

[WebMethod(Description="Specify the product ID, product name, unit price, and quantity to add it to the Sales catalog")] public string AddProduct(string PID, string ProductName, int


return "Please specify a valid value for price"; if (Qty<0)

return "Please specify a valid value for quantity"; sqlConnection1.Open();

sqlCommand1.CommandText="INSERT INTO Products(ProductID, ProductName, UnitPrice, QtyAvailable) VALUES ('" +

"', '" + ProductName + "', '" + Price + "', '" + Qty



return "Record updated successfully";

catch(Exception e) {

return e.Message;

The preceding code uses the sqlConnection1 control to add a record to the Products table. When you add records to the Web service, you use the values that are supplied to the AddProduct function as parameters.

As you code the AddProduct method of the Web service, you can code other methods to retrieve product details in the Products database or perform other custom actions. To test the Web service after you add the methods to it, follow these steps:

1. Select Build ^ Build Solution to build the Web service.

2. To start debugging the Web service, select the Debug ^ Start menu option.

The Web service opens in Internet Explorer. The first dialog box displays the methods that you coded for your Web service. To test the AddProduct method, follow these steps:

1. Click AddProduct on the Service1 Web Service dialog box.

2. The AddProduct dialog box opens, as shown in Figure 28-4. In this dialog box, you can invoke the AddProduct function after supplying the required parameters to test its output.


Fit Yfc* Mb r„k in»

™ _> _ll ¡t^ncrm h* fli+fip jjr* >j 1" J

3 L J1 [-(U rn"iaiinvri i hyi mn - vp pt"-^*-. i i -a • -xmufival v'l

jjJiifi i]





Ci* 1 rrr'tar « wiV* 'jt^ li

AddPrtKfucf rc v-t< H* C". r rz-M r^-rr-t.tA^.trc*. ft -3 tï vM * «■? «+-F 1 1 Ta it

PMiitvmeUi V+vt

■ ¡0 : |


Vwrti |

TTW Nh!Pr*p FI * tM* r*wp#*id rpiprrw* 711

— MVbnfMiikiU

jj i

Figure 28-4: Specify the parameters for the AddProduct function in order to test it.

3. Type the parameters for PID, ProductName, Price, and Qty as P002, PDA, 100, 200, respectively, and click Invoke to test its output.

4. When the record is added successfully to the Web service, you see the output displayed in Figure 28-5.

J J!. -2 L^B^ST J lr_J - J»ii ln-1 ■ ivu-itt-iih-lî mi-vitvin-^.-v*-*-!'- i-rt.-iih1-v.k«1 hi'ihil ik .ïiru ifvwf^wtf liv»- de lrk-i b

: lu rf = ni y //h..' ■ ■ - tiki * "ti /l'i-- ■ --i i • I'W^H.I ■UAI.ij'»'^-9'f

Figure 28-5: Output such as this appears when a record is successfully added to the Web service.

After you successfully test your Web service, you can create a Web service client to access the Web service.

0 0

Post a comment