Creating an ADO.NET Shopping Cart • Chapter 12 |
521 |
While in design mode, you can drag and drop this connection onto your
.asmx page.This will add the following to our code-behind page as the first line in the service public class:
private System.Data.OleDb.OleDbConnection oleDbConnection1;
Connection string information will also be added to the InitializeComponent() method. Alternatively, we can still create a connection string by creating a .udl file on the desktop, double-clicking it and following the dialogs.With this method, we will have to insert the code ourselves, as follows:
1.In C#, add Using System.Data.OleDb to the top “using” section.
2.Then add the following inside the service class:
private OleDbConnection myConnection = new OleDbConnection();
3.Add the following to a method (Page_onload, or a method of your own creation):
myConnection.ConnectionString =
[the string obtained from the udl file]
We will take a closer look at adding a connection when we create the “sellerAdmin” service in the next section.
Creating a Web Service
All of the code for the Web Services in this chapter can be found on the CD. (See adminCustomer.asmx.cs, sellerAdmin.asmx.cs, getBooks.asmx.cs, getCategories.asmx.cs, getCustomer.asmx.cs, loginCustomer.asmx.cs, orderBooks.asmx.cs, and sellerAdmin.asmx.cs.)
Let’s take a closer look at adding a connection by creating the “sellerAdmin” Service.To create this service follow these steps:
1.Create the connection object.
2.Set the connection string.
3.Create the Command object.
4.Create the Parameter objects and assign their values.
5.Execute the procedure.We will be using the AdminAddBook proc. It takes the following parameters: BK_ISBN, BK_Author, BK_Price,
BK_Title, BK_Description, CAT_ID, BK_ImagePath.
6.Return string indicating success or failure of the operation.