Tuesday, August 28, 2012

Silverlight RIA Services Data

Silverlight RIA Services Data

I've been trying to figure out how to access data from my Silverlight application for a few days, now.

I want to use a data class and a business class already written.

Based on some advice from another post I created a Silverlight Business application. The Code to access the data is in a Domain Service class in my Web Application. This is called from the Silveright application.

I think I'm close but I don't have the syntax quite right.

Here is the code in my Domain Service Class

Public Function GetGridData() As IEnumerable(Of Submissions)  Dim dtResults As DataTable  Dim _ConnectionString As String = _  "Password=xxxx;Persist Security Info=True;User ID=xxxx;Initial Catalog=APCD;Data Source=xxxx"  mdsResults = s.GetSubmissions(3, 0, _ConnectionString,"2011", "0", False)  dtResults = mdsResults.Tables(0)  Dim MySubmissions = New List(Of Submissions )()  For Each row As DataRow In dtResults.Rows  Dim MySubmission = New Submissions() With { _ .SubmissionControlId = Convert.ToString(row("SubmissionControlId" )), _ .OrgId = Convert.ToString(row("Org Id" )), _ .DateProcessed = Convert.ToString(row("DateProcessed")) _  }   MySubmissions.Add(MySubmission)  Next  Return MySubmissions  End Function  The code in the silverlght page is     Dim x As New Web.CustomerDomainContext      grdSubmissions.DataContext = x.GetGridData() 

It all compiled and runs but the grid is empty. I know from stepping through that Stored Procedure does contain data.

Answers & Comments...

Answer: 1

There are several points I'd like to comment on.

Firstly, it is better not to return from a function like above. This is because you lose the benefit of being able to edit and update data. You should return IQueryable instead. Generate some code using EF to have a feel for it.

Secondly, instead of setting DataContext, the ItemsSource on the DataGrid instead.

Dim gridData = x.GetGridData() grdSubmissions.ItemsSource= gridData 

Thirdly, you should also step through Silverlight code and make sure gridData contains values. From first observation, it doesn't look correct because Silverlight is asynchronous. The code to fetch data from grid should look like:

Dim domainContext = new MyDomainContext() AddHandler domainContext.Completed,    Sub (op)     grdSubmissions.ItemsSource = domainContext.Submissions   End Sub  domainContext.Load(domainContext.GetSubmissionQuery()) 

You are best working through this example: http://blogs.msdn.com/b/kylemc/archive/2011/04/29/mvvm-pattern-for-ria-services.aspx as the original Ria Services code is the bare minimum, and needs more convenience methods.

by : Chui Teyhttp://stackoverflow.com/users/34461




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog