Solved

Fill ObservableCollection with database query

Posted on 2012-03-22
3
2,730 Views
Last Modified: 2013-11-12
I am using Silverlight 4 and SharePoint and want to fill an ObservableCollection from a returned dataset from SharePoint. I have the query and connection to SharePoint done. In my example my List is called _getList and my ListCollectionItems is called _getListItems. My query returns a dataset of x amount of rows of data from an Employee table.

How do I iterate through the returned dataset (_getListItems) and populate Silverlight's ObservableCollection so that I may perform binding and validation on the controls (i.e. textboxes, listboxes, comboboxes, etc.)?
0
Comment
Question by:sr2007
3 Comments
 
LVL 22

Expert Comment

by:ambience
ID: 37775346
Using untyped datasets?

Can you change the query to return an Observable collection instead? (if thats in your control)

Alternatively, you may have to create a model class and loop through returned rows to populate the observablecollection. This shouldn't be difficult, however two way binding may require sending updates back to the database?

Check this

http://social.expression.microsoft.com/Forums/en/blend/thread/3772bf00-06a2-466a-8a56-49ed8c5a471b

It shows how to fill a List, but can be changed to return an ObservableCollection instead.
0
 
LVL 4

Accepted Solution

by:
Sathish DV earned 500 total points
ID: 37826917
Dear sr2007,

Since u already have List or ListCollectionItem with entire dataset collection, u can better include one extension method so the List will be returned as ObservableCollection type.

Below is extension method to convert List to Observable collection

public static class ExtensionMethods
    {
        public static ObservableCollection<T> ToObservableList<T>(this IEnumerable<T> data)
        {
            ObservableCollection<T> dataToReturn = new ObservableCollection<T>();

            foreach (T t in data)
                dataToReturn.Add(t);

            return dataToReturn;
        }
    }

So while returning List or ListCollection u can use the below code

Consider ur dataset returns Employee type....

public ObservableCollection<Employee> GetEmp()
{
      return getList.ToObservableList();
}

Let me know if you need any other information.

By doing this u need not to re-write most of the code... just create one Extension method and use it where ever needed.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

We had a requirement to extract data from a SharePoint 2010 Customer List into a CSV file and then place the CSV file into a directory on the network so that the file could be consumed by an AS400 system. I will share in Part 1 how to Extract the Da…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now