Skip to main content

Microsoft Silverlight

Answered Question how to get data from the serverRSS Feed

(0)

samw
samw

Member

Member

67 points

124 Posts

how to get data from the server

How do I effect a transfer of data from the server to the client using the following code?  Do I need to write a method on the server side?

Note that the class FeedbackView does not exist in my DataModel.

 

 

public FeedbackPage()
{
InitializeComponent();

PersonalWebsiteDomainContext context = new PersonalWebsiteDomainContext();
this.DataContext = context;

List query = (from f in context.Feedbacks
join a in context.Articles on f.ArticleID equals a.ArticleID
select new FeedbackView
{
FeedbackID = f.FeedbackID,
ParentID = f.ParentID,
ArticleID = f.ArticleID,
ArticleName = a.Title,
Date = f.Date,
SenderName = f.SenderName,
SenderIPAddress = f.SenderIPAddress,
Comment = f.Comment,
Approved = f.Approved
}).ToList();

dg.ItemsSource = query;
}


protected class FeedbackView
{
public int FeedbackID {get; set;}
[Key]
public int ParentID {get; set;}
public int ArticleID {get; set;}
public string ArticleName {get; set;}
public DateTime Date {get; set;}
public string SenderName {get; set;}
public string SenderIPAddress {get; set;}
public string Comment {get; set;}
public bool Approved { get; set; }
}
  

codenenterp
codenenterp

Member

Member

36 points

13 Posts

Re: how to get data from the server

you will have to write some type of service for the silverlight client to call and get the data.  The call to the server is async so dont be suprised.

Solutions Architect
Coden Enterprises
http://www.codenenterprises.com/iblog

theo67
theo67

Member

Member

524 points

177 Posts

Re: how to get data from the server

You need to call a method on the context before you create "query" else "context.Feedback" will be empty. Normally you could create a LINQ-query and pass it to the DomainContext. Another way is to implement a specific query method in the DomainService which will be exposed via the DomainContext and cal be called in your client. See the QueryAttribute. Anyway when you use the DomainDataSource you can declare this in XAML.

Have you considered using a viewmodel that wraps the PersonalWebsiteDomainContext, like http://stackoverflow.com/questions/744474/combining-net-ria-services-and-mvvm-in-silverlight-3-0. This way you move the query in the viewmodel.

Theo.

ippon11
ippon11

Member

Member

24 points

7 Posts

Re: how to get data from the server

 Alternatively Sam, for a quick mock-up (unsecure), you can create xml as your data, and either embed it as a resource in your SL project, or use a webclient to retrieve it from a remote location via http.  You can then use your existing code to query the xml as an xmldatasource.  This not a productised solution but will get you up and running quickly to prototype the rest of your SL app.

 Michael

 

...Mark this as answer if it has been helpful.

samw
samw

Member

Member

67 points

124 Posts

Re: how to get data from the server

 Thanks all for the replies.  I was on vacation this weekend.  
OK here is what I did.  It mostly works but I'm not sure it's the best way.  The problem I'm having is using the IQueryable object. See commented section of code where I set the ItemsSource of the datagrid.

1.) Server:  Made a file for my "...View" classes:

 

namespace PersonalWebsite1.Web
{
public class FeedbackView
{
[Key]
public int FeedbackID { get; set; }
public int ParentID { get; set; }
public int ArticleID { get; set; }
public string ArticleName { get; set; }
public DateTime Date { get; set; }
public string SenderName { get; set; }
public string SenderIPAddress { get; set; }
public string Comment { get; set; }
public bool Approved { get; set; }
}
}
2.) Server: marked my DomainService class with the partial modifer (Why is this class not generated with the partial modifier?).  Created another file, same class, like this:

 

namespace PersonalWebsite1.Web
{
public partial class PersonalWebsiteDomainService : LinqToEntitiesDomainService

{
public IQueryable GetFeedbackView()
{
var query = (from f in Context.Feedback
join a in Context.Articles on f.ArticleID equals a.ArticleID
select new FeedbackView
{
FeedbackID = f.FeedbackID,
ParentID = f.ParentID,
ArticleID = f.ArticleID,
ArticleName = a.Title,
Date = f.Date,
SenderName = f.SenderName,
SenderIPAddress = f.SenderIPAddress,
Comment = f.Comment,
Approved = f.Approved
}).AsQueryable();

return query;
}
}
}
  

3) Client:  Set the ItemsSource of the datagrid as follows:

  

namespace PersonalWebsiteAdmin.Views
{
public partial class FeedbackPage : BasePage
{
public FeedbackPage()
{
InitializeComponent();
PersonalWebsiteDomainContext Context = new PersonalWebsiteDomainContext();

//Does not work.
IQueryable query = Context.FeedbackViews.AsQueryable().Where(f => f.FeedbackID < 7);
Context.LoadFeedbackView(query,null);
dg.ItemsSource = query;


// Works
//Context.LoadFeedback();
//dg.ItemsSource = Context.Feedbacks;

}
 

 

theo67
theo67

Member

Member

524 points

177 Posts

Answered Question

Re: how to get data from the server

Have you tried binding to the Context.Feedbacks?

Context.Loaded += new EventHandler< System.Windows.Ria.Data.LoadedDataEventArgs >(OnDataLoaded);
IQueryable query = Context.FeedbackViews.AsQueryable().Where(f => f.FeedbackID < 7);
Context.LoadFeedbackView(query,null);
...
void OnDataLoaded(object sender, System.Alexandria.Data.Client.DataLoadedEventArgs e)
{
Context.Loaded -= new EventHandler< System.Windows.Ria.Data.LoadedDataEventArgs >(OnDataLoaded);
 dg.ItemsSource = Context.Feedbacks;
}
 

This LoadFeedbackView() will result in an async request, so the Context.Feedbacks won't be populated immediately.

Theo.

samw
samw

Member

Member

67 points

124 Posts

Re: how to get data from the server

 Excellent! Thank you Theo.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities