using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
namespace WebServices
{
public partial class CustomerSvcExample :
System.Web.UI.Page
{
CustomerSvc.CustomerService pxy = new CustomerSvc.CustomerService();
protected void Page_Load(object sender, EventArgs e)
{
lblDisplay.Text = "";
}
protected void btnDisplayCustomers_Click(object sender, EventArgs e)
{
gvCustomers.DataSource
= pxy.GetCustomers();
gvCustomers.DataBind();
}
protected void btnAddCustomer_Click(object sender, EventArgs e)
{
// Create an object of the Customer class which is avaialable through the web service reference and WSDL
CustomerSvc.Customer objCustomer
= new CustomerSvc.Customer();
objCustomer.Name = txtName.Text;
objCustomer.Address = txtAddress.Text;
objCustomer.City = txtCity.Text;
objCustomer.State = txtState.Text;
objCustomer.Zip = int.Parse(txtZip.Text);
objCustomer.Email = txtEmail.Text;
// Use
the web service proxy to send a Customer object that will be used by the web
service method
// to
store a new record in the database.
if (pxy.AddCustomer(objCustomer))
{
lblDisplay.Text
= "The customer was added
successfully. Name: " + objCustomer.Name;
}
else
{
lblDisplay.Text
= "A problem occurred while adding the
customer to the database. The data wasn't recorded.";
}
}
protected void btnFindCustomer_Click(object sender, EventArgs e)
{
// Find
a record in the database using the web service method that returns a Customer
object
CustomerSvc.Customer objCustomer = pxy.GetCustomerByName(txtName.Text);
if (objCustomer != null)
{
//
Use the object's values to populate the form's controls.
txtName.Text
= objCustomer.Name;
txtAddress.Text
= objCustomer.Address;
txtCity.Text
= objCustomer.City;
txtState.Text
= objCustomer.State;
txtZip.Text
= objCustomer.Zip.ToString();
txtEmail.Text
= objCustomer.Email;
}
else
{
lblDisplay.Text
= "No records found.";
}
}
protected void btnFindCustomers_Click(object sender, EventArgs e)
{
//
Retrieve the arraylist containing all the customers
returned by the web servier method via proxy.
ArrayList
customers = new ArrayList(pxy.GetCustomersByName(txtName.Text));
// Bind
the arraylist to the GridView
to display all the customers with a given name.
gvCustomers.DataSource
= customers;
gvCustomers.DataBind();
txtName.Text
= "";
txtAddress.Text
= "";
txtCity.Text
= "";
txtState.Text
= "";
txtZip.Text
= "";
txtEmail.Text
= "";
if (customers.Count == 0)
{
lblDisplay.Text
= "No records found.";
}
}
}
}