This article has been
excerpted from book "A Programmer's Guide to ADO.NET in C#".
Now, why don't you ask the web service to do some more work than just returning
data from a database table?
This time I'm going to write to the database. I read data from a client in the
form of an array, place it in a dataset, and then add the data to the orders
table of the Northwind database.
To do so, I open the OrderRetrievalService project and generate a dataset by
using right-click > Generate DataSet option of the data adapter. The Generate
DataSet option of the date adapter generates a typed dataset and adds the class
DataSet1, which from the dataset.
Now I add a new web method called InsertOrder (see Listing 8-5). In this method,
I read data from a database using a data adapter and filled data from the data
adapter to a dataset using the data adapter's Fill method. As you can see from
Listing 8-5, I've used a SELECT * query with ORDER BY for the OrderId column
values.
It gets the last OrderId in the table to determine what is the next available
OrderId value. Then it creates a new DataRow in the dataset and populates the
row with data passed in from the client. Finally, it calls the Update method of
the data adapter to save the new row into the database.
Listing 8-5 Web service method for populating an order in the database
[WebMethod(Description = "Insert Order from
Array")]
public int
InsertOrder(string[] OrderInfo)
{
DataSet1 ds = new DataSet1();
oleDbDataAdapter1.SelectCommand.CommandText =
"select * from orders ORDER BY OrderId";
oleDbDataAdapter1.Fill(ds, "Orders");
DataRow drLast =
ds.orders.Rows[ds.orders.Rows.Count - 1];
int LastOrderId =
Convert.ToInt32(drLast["OrderId"]);
DataSet1.ordersRow dr = ds.orders.NewordersRow();
dr.OrderID = LastOrderId + 1;
dr.OrderDate = Convert.ToDateTime(OrderInfo[0]);
dr.ShipName = OredrInfo[1];
dr.ShipAddress = OredrInfo[2];
dr.ShipCity = OredrInfo[3];
dr.ShipCountry = OredrInfo[4];
dr.ShipPostalCode = OredrInfo[5];
ds.orders.AddordersRow(dr);
oleDbDataAdapter1.Update(ds, "Orders");
return dr.OrderID;
}
Now build and test the service. This service now has two methods, as shown in
Figure 8-21.
Figure 8-21. The InsertOrder and GetOrderFromDatabase methods of
OrderRetrievalService
Now it's time to test your newly added Web method InsertOrder. To test this
service, follow the same steps as in the previous section when testing your
GetOrderFromDataSet method.
Create a web application project using VS.NET. You can even use the same client
you used to test the GetOrderFromDatabase method. Create a new project called
AddOrderClientApp (see Figure 8-22).
Figure 8-22. Creating a client to test the InsertOrder web method
The next step is to add a Web reference to the Web service by right-clicking on
the References node of a project in the Solution Explorer and clicking on the
Add Web reference option.
Note: If you're using same client application you used last time, you
need to remove the namespace and then add it again by using the add Web
Reference option to get the latest updated Web service contents.
Again, follow the same steps as in the last sample. Type the URL of your service
in the Address text box and click the Add Reference button, which adds the mcb
namespace (or localhost if you're using default localhost as the web server) to
the project. Add Reference also generates the *.disco, *.wsdl, and proxy files.
Now you can call your Web service. Add some controls to the page to make it look
like Figure 8-23. I added six text boxes to accept the order date, name,
address, city, country, and zip of a customer. The enter order button submits
the order to the service.
Figure 8-23. Design view of the order customer entry web service
Again, double-click on the Enter Order button use the code in listing 8-6 to
populate the order array and pass it to the Web service. The enter order button
event handler looks like listing 8-6.
Listing 8-6. Client event handler for executing the Web service
private void
Button1_Click(object sender, System.EventArgs
e)
{
string[] orderData =
new string[5];
orderData[0] = TextBox1.Text;
orderData[1] = TextBox2.Text;
orderData[2] = TextBox3.Text;
orderData[3] = TextBox4.Text;
orderData[4] = TextBox5.Text;
mcb.Service1 myWebService = new
mcb.Service1();
myWebService.InsertOrder(orderData);
}
The button event handler assigns a strings array with information typed into the
order form. You can create an instance of the service by directly using
mcb.Service1 and calling the InsertOrder method. If you're using localhost as
your Web server, the code would look like the following:
localhost.Service1 myWebService = new
localhost.Service1();
myWebService.InsertOrder(orderData);
As you can see from figure 8-24, the InsertOrder method is available in the
list.
Figure 8-24. The InsertOrder method available in the list
Now build and run the application. The output looks like figure 8-25. Now you
enter customer data and clicking the enter order button adds the data to the
orders table.
Figure 8-25. Adding an order to the database using Enter order
Conclusion
Hope this article would have helped you in understanding
adding
Functionality to the Web Service. See other articles on
the website also for further reference.
|
This essential guide
to Microsoft's ADO.NET overviews C#, then leads you toward deeper
understanding of ADO.NET. |