Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 752
casillas Avatar asked Dec 05 '25 19:12

casillas


1 Answers

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.

like image 100
Pete Avatar answered Dec 08 '25 13:12

Pete



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!