Monday, November 24, 2008

Silverlight+ADO.net Data Service (2)

Last time, we talk about how to build the ado.net data service server side development. There are plenty of posts on how to build the ado.net dat service server side application. Scott Gu's blog is one of the most classical one. For the client side, we are going to use a data grid as the component.

In client side, reference, add service reference.

Let's put in the page.xaml some code to place the buttons, data grid, text box et al, and you can design your own in Expression Blend to make the controls beautiful.

ex1. Get Data
private void ButtonSelect_Click(object sender, RoutedEventArgs e)
{
ServiceReference.DataEntities ws = new ServiceReference.DataEntities(new Uri("MyDataService.svc", UriKind.Relative));
var query = (from p in ws.CustomerSet select p);
DataServiceQuery userQuery = (DataServiceQuery)query;
userQuery.BeginExecute(new AsyncCallback(OnLoadComplete), query);
}
void OnLoadComplete(IAsyncResult result)
{
DataServiceQuery query = (DataServiceQuery)result.AsyncState;
dataGrid.ItemsSource = query.EndExecute(result).ToList();
}

The above example is show on my APM essay.

ex 2. Insert Data
We can follow msdn: http://msdn.microsoft.com/en-us/library/cc903944(VS.95).aspx to write code.
We drag four text box to the page.xaml, and an insert button. The code a like below:


//Insert Record by Text Box
private void InsertClick(object sender, RoutedEventArgs e)
{
ServiceReference.DataEntities ws = new ServiceReference.DataEntities(new Uri("MyDataService.svc", UriKind.Relative));
Customer ds = new Customer();

ds.ProductID = Int32.Parse(PIDText.Text);
ds.ProductName = PNText.Text;
ds.Price = PRText.Text;
ds.ShippingConfirmNumber = SNText.Text;
ws.AddToDataSources(ds);
ws.BeginSaveChanges(OnSaveCompleted, ws);

}

void OnSaveCompleted(IAsyncResult result)
{

ServiceReference.DataEntities wsd = (ServiceReference.DataEntities)result.AsyncState;
ObservableCollection obds = new ObservableCollection();
obds.Add(result.AsyncState as Customer);
wsd.EndSaveChanges(result);

}
For this code, there are new data type ObservableCollection, it is a dynamic data type coming after WPF. It allows us to do dynamic data binding. For more details, we can go to msdn help document for this data type.

ex 3. Update and Delete

//Update DataGrid Record
private void UpdateClick(object sender, RoutedEventArgs e)
{

DataEntities de = new DataEntities(new Uri("MyDataService.svc", UriKind.Relative));
Customerds = (Customer)dataGrid.SelectedItem;
de.AttachTo("CustomerSet", ds);
de.UpdateObject(ds);
de.BeginSaveChanges(OnUpdate, de);
}

void OnUpdate(IAsyncResult result)
{
ServiceReference.DataEntities wsd = (ServiceReference.DataEntities)result.AsyncState;
ObservableCollection obds = new ObservableCollection();
obds.Add(result.AsyncState as Customer);
wsd.EndSaveChanges(result);
}


//Delete DataGrid Record
private void DeleteClick(object sender, RoutedEventArgs e)
{
ServiceReference.DataEntities ws = new ServiceReference.DataEntities(new Uri("MyDataService.svc", UriKind.Relative));
Customer ds = (Customer)dataGrid.SelectedItem;
ws.AttachTo("CustomerSet", ds);
ws.DeleteObject(ds);
ws.BeginSaveChanges(OnUpdate, ws);
}
void OnDelete(IAsyncResult result)
{
ServiceReference.DataEntities wsd = (ServiceReference.DataEntities)result.AsyncState;
ObservableCollection obds = new ObservableCollection();
obds.Add(result.AsyncState as Customer);
wsd.EndSaveChanges(result);

}

Notice the msdn code does not work for the link: http://msdn.microsoft.com/en-us/library/cc903956(VS.95).aspx

There are 3 things the msdn code should be changed:
(1) We should use an AttachTo() method in BeginSaveChanges block. And attach our created data class to the data set.
(2) ws.BeginSaveChanges(OnUpdate, ws); The second parameter should be the service reference object or entity proxy object, what we updated here are the service reference, not the data class we created.
(3) In the EndSaveChanges block, we should use ObservableCollection, the dynamic collection data type.

No comments: