- Code: Select all
public class Employees : _Employees
{
public void FromDataSet(DataSet ds)
{
this.DataTable = ds.Tables[0];
}
public DataSet ToDataSet()
{
DataSet dataSet = new DataSet();
dataSet.Tables.Add(this.DataTable);
return dataSet;
}
}
Next, I created my web service which does only two things, LoadAll and Save. Notice that my web service uses the two methods I added in my concrete class ToDataSet and FromDataSet
- Code: Select all
public class EmployeesService : System.Web.Services.WebService
{
[WebMethod]
public DataSet LoadAll()
{
try
{
Employees emps = new Employees();
emps.LoadAll();
return emps.ToDataSet();
}
catch { return null; }
}
[WebMethod]
public bool Save(DataSet dataset)
{
bool wasSaved = false;
try
{
Employees emps = new Employees();
emps.FromDataSet(dataset);
emps.Save();
wasSaved = true;
}
catch { }
return wasSaved;
}
}
My next step was to test it. I tested the following code snippet in both a web form and win form, it worked fine in both cases.
- Code: Select all
//----------------------------------------------------------
// Invoke the Web Service and Load all of the Data
//----------------------------------------------------------
EmployeesService empsService = new EmployeesService();
DataSet ds = empsService.LoadAll();
//----------------------------------------------------------
// Load that Data into a local Employees object
//----------------------------------------------------------
Employees emps = new Employees();
emps.FromDataSet(ds);
emps.FirstName = \"WebService!!\";
emps.GetChanges(); // very important
//----------------------------------------------------------
// Reconvert to a DataSet (after calling GetChanges)
//----------------------------------------------------------
ds = emps.ToDataSet();
//----------------------------------------------------------
// Marshall it back to the web service
//----------------------------------------------------------
empsService.Save(ds);
Notice that I call GetChanges(). This reduced the data to only modified, inserted, or deleted rows, this way you only marshall back to the web service data that has changed.
There are more elegant solutions I'm sure, if I were going to support a query mechanism in my web service I would use the LoadFromRawSql() method and syntax. I would also add those two methods (FromDataSet and ToDataSet) to my concrete class template and regenerate.
