In Windows 8 Application development, we came across the “Value does not fall within the expected range.” exception in two scinarios.
Scenario 1: We have a CollectionViewSource defined in our XAML called sampleData
<CollectionViewSource x:name=”sampleData” />
The source of the CollectionViewSource is set using the following code
sampleData.Source=Utility.GetData(custId);
where we are getting the data from an asynchronous method, GetData in the Utility class.
public async static Task<List<Data>> GetData(string data)
{
………………………………
……………………………….
……………………………….
}
Solution:
Call the asynchronous method using await
sampleData.Source=await Utility.GetData(custId);
Scenario 2: Another scenario, where we used the following code
<CollectionViewSource x:name=”sampleData” />
sampleData.Source=await Utility.GetData(custId);
public async static Task<Data> GetData(string data)
{……………………………………………………………………….}
Solution:
CollectionViewSource requires a collection data source. In our sample, it returns an object of type Data.
public async static Task<List<Data>> GetData(string data)
{……………………………………………………………………….}