0

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.

Bob Avallone
  • 379
  • 1
  • 10
  • 29

1 Answers1

0

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.

Update 1 - DomainService

namespace SampleDataClass.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;

    public class Customer
    {
        [Key]
        public int CustomerId { get; set; }

        public string Name { get; set; }

        public string Address { get; set; }
    }

    // TODO: Create methods containing your application logic.
    [EnableClientAccess()]
    public class DomainService1 : DomainService
    {

        [Query]
        public IQueryable<Customer> GetCustomers()
        {
            return (new Customer[]
            {
                new Customer() { CustomerId=1, Name="Luigina Davide", Address="Via Giulio Petroni, 82 24050-Palosco BG" },
            }).AsQueryable<Customer>();
        }
    }

}
Chui Tey
  • 5,436
  • 2
  • 35
  • 44
  • Chui, thanks very much for answering. Regarding your first point if I change Ienumerable to Iqueryable, the code does not compile into the generated code which means I cannnot reference it in the silverlight project. Regarding the second point, I did try using ItemsSource when I do I get a conversion error. Regarding the third point I cannot get it to compile and the sample project I cannot figure out how to relate to my problem. So I still need help if Chui or anyone else is willing. Bob – Bob Avallone Aug 28 '12 at 18:27
  • Bob, Make sure you have a RIA services link http://msdn.microsoft.com/en-us/library/ee707372(v=vs.91).aspx What project template did you use anyway? – Chui Tey Aug 28 '12 at 21:40
  • I am using a Silverlight Business Application. I checked and the RIA services link is automatically being set. - Bob – Bob Avallone Aug 30 '12 at 13:58