Displaying, adding, deleting, and editing contact records
require an AJAX service to be able to interact with the database. The database
is very simple and contains a single table named Contacts.
The contacts table contains, ContactID, FirstName, LastName,
and Email columns.
The AJAX service is defined as follows:
Listing 17
namespace Bhaidar.Services
{
[ScriptService]
public class ContactsService : System.Web.Services.WebService
{
The major thing to notice is the addition of the attribute
ScriptService. This attribute is the one that allows the server to be a
script-callable service.
The Web service contains the following web methods.
Listing 18
public System.Collections.Generic.List<Contact> GetContactList
(int startIndex, int maximumRows)
{
return ContactsManager.Instance.GetContactList(startIndex, maximumRows);
}
The above web method simply returns a generic list of
contacts from the database. It has a start index and maximum rows input
parameters that allow you, if you want, to implement paging inside the grid
client control.
The web method calls the ContactsManager class that contains
all the details of connecting to the database. The code is very simple, you can
follow it by downloading the accompanying code of this article.
Listing 19
[WebMethod]
public int AddNewContact(string firstName, string lastName, string email)
{
return ContactsManager.Instance.AddNewContact(firstName, lastName, email);
}
The above Web method is used to add a new contact to the
database. It takes as input the first name, last name and email input
parameters and internally calls the ContactsManager’s AddNewContact method to
add the new contact to the database.
Listing 20
[WebMethod]
public bool UpdateContact(int contactID, string email)
{
return ContactsManager.Instance.UpdateContact(contactID, email);
}
The above Web method is used to update an existing record in
the database. The method takes as input the contact ID and the email address, which
means, only the email address is allowed to be updated. Internally, the method
calls the ContactsManager’s UpdateContact method to do the actual update on the
database.
The last Web method is the DeleteContact.
Listing 21
[WebMethod]
public bool DeleteContact(int contactID)
{
return ContactsManager.Instance.DeleteContact(contactID);
}
It takes as input the contact ID to be deleted and calls
internally the ContactsManager’s DeleteContact method.