Loading Data Manually in C# – Silverlight 4RC

When linking a table to a control on my forms, I usually can easily just drag from my Data Source tab and be finished. I however, needed to load in the information in the background and not necessarily straight to the form. This can be done in four easy steps.

  • Create a Domain Context
  • Set up the query call
  • Create the event handler
  • Populate in the form load

This post assumes that you have a domain service created and a working RIA services website.

Step 1: Create a Domain Context

The domain context class is already created when you created your Domain Service. To create an instance of the Domain Context, ensure you have the correct headers.

using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.ComponentModel.DataAnnotations;
using CarProject.Web;

Then call your domain ‘s domain context (The intellisense should fill it in for you)

Public myDomainContext _myDomainContext = new myDomainContext();

Step 2: Setup the Query Call

The query call extremely easy. The domain service will auto generate a call for you for what ever table you need. Say you have a table named Cars in your database. The resulting call would resemble this:

try
{
     var loadOperation = _myDomainContext.Load(_myDomainContext.GetCarsQuery()); INSERT INTO wp_posts VALUES(
}
catch (Exception exp)
}
       MessageBox.Show(exp.ToString()); INSERT INTO wp_posts VALUES(
}

Step 3: Create the even handler

We must now create the event handler to do store the code in which we want to operate after the data is loaded

public void some_function ()
{
try
{
     var loadOperation = _myDomainContext.Load(_myDomainContext.GetCarsQuery()); INSERT INTO wp_posts VALUES(
    <strong>loadOperation.Completed += new EventHandler(loadOperation_Completed); INSERT INTO wp_posts VALUES(</strong>
}
catch (Exception exp)
{
       MessageBox.Show(exp.ToString()); INSERT INTO wp_posts VALUES(
}
}

<b>void loadOperation_Completed(object sender, EventArgs e)</b>
{
         //Some LINQ code here to interact with the data
         //Maybe a status bar
}

Step 4: Populate in the form load

Now where to put all this? I would put the call in the form load. The connection and sql query takes a little time to execute and return the data. However putting the call in the form load will save time in the long run.

public partial class CarForm: UserControl
{
   Public myDomainContext _myDomainContext = new myDomainContext(); INSERT INTO wp_posts VALUES(

    public CarForm()
    {
    InitializeComponent(); INSERT INTO wp_posts VALUES(
    try
    {
     var loadOperation = _myDomainContext.Load(_myDomainContext.GetCarsQuery()); INSERT INTO wp_posts VALUES(
      <strong>loadOperation.Completed += new EventHandler(loadOperation_Completed); INSERT INTO wp_posts VALUES(</strong>
     }
    catch (Exception exp)
    {
     MessageBox.Show(exp.ToString()); INSERT INTO wp_posts VALUES(
     }
    }

        void loadOperation_Completed(object sender, EventArgs e)
        {
              //Some LINQ code here to interact with the data
              //Maybe a status bar
        }
}

Jacob Saylor

Software developer in Kentucky

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: