Search code examples
xamarinodataxamarin.forms

OData Connection in Xamarin Form


My code crashes and gives the following error on simulator. It attempts to run the try block in the GetDataFromOdataService() method and throws an error and also issue an alert. I am using Xamarin.Form

using Simple.OData.Client;
using System.Threading.Tasks;

      private ODataClient mODataClient;

   protected async override void OnAppearing ()
    {
        base.OnAppearing ();
        await InitializeDataService ();
        await GetDataFromOdataService();
    }

     public async Task <bool> InitializeDataService(){

            try {
                mODataClient = new ODataClient ("http://services.odata.org/Northwind/Northwind.svc/");

            }

            catch {
                await DisplayAlert("Error", "Connection Error", "OK", "Cancel");
                System.Diagnostics.Debug.WriteLine("ERROR!");

            }
            return true;
        }

    public async Task<bool> GetDataFromOdataService (){

            try {

                myCustomers= await mODataClient.For("Customers").Top(10).FindEntriesAsync();

            }

            catch {
                await DisplayAlert("Error", "Connection Error", "OK", "Cancel");
                System.Diagnostics.Debug.WriteLine("ERROR!");

            }

            return true;
        }

enter image description here


Solution

  • Couple issues:-

    In the constructor it was doing var list = new ListView() which constrained it locally than setting the class level scope variable. This was therefore adjusted to list = new ListView().

    The other thing, was in the GetTheData function where the items source was being assigned as list.ItemsSource = myList; where it needed changing to list.ItemsSource = Customers;.

    I've repackaged the zip file up and sent to you. Let me know if this works for you? You should now be able to see all your customers in the ListView.