Link to home
Start Free TrialLog in
Avatar of Thomas Swaney
Thomas SwaneyFlag for United States of America

asked on

Help with example code for Xamarin.Forms

I am new to Xamarin, C# and SQLite. I found a article https://msdn.microsoft.com/en-us/magazine/mt736454.aspx.
Basically I can get the Android code to work properly but I can't get the IOS code to work.
The emulator starts on the mac but then I get the error that you see in the attached file. I understand that it is due to something not getting passed but don't understand what that is.

Any help would be greatly appreciated so that I can move forward with this project.

Thanks in advance
SQLite_IOS_Error.png
Avatar of Lucas Leonel Galetti
Lucas Leonel Galetti
Flag of Argentina image

Maybe you should instance that object like:

database = new SQLiteConnection();

Open in new window

or maybe
SQLiteConnection database = new SQLiteConnection();

Open in new window

Avatar of Thomas Swaney

ASKER

I changed the code in the CustomersDataAccess class to
        public CustomersDataAccess()
        {
            SQLiteConnection database = DependencyService.Get<IDatabaseConnection>().DbConnection();
//            database =
//                    DependencyService.Get<IDatabaseConnection>().
//                    DbConnection();
            database.CreateTable<Customer>();

            this.Customers =
                new ObservableCollection<Customer>(database.Table<Customer>());
            // If the table is empy, initialize the collection
            if (!database.Table<Customer>().Any())
            {
                AddNewCustomer();
            }
        }

Open in new window


It still doesn't work with IOS but works with Android.

I feel that it has something to do with either the DatabaseConnection_Android class or the DatabaseConnection_IOS class.

DatabaseConnection_Android.cs
using SQLite;
using LocalDataAccess.Droid;
using System.IO;
[assembly: Xamarin.Forms.Dependency(typeof(DatabaseConnection_Android))]

namespace LocalDataAccess.Droid
{
    public class DatabaseConnection_Android : IDatabaseConnection
    {
        public SQLiteConnection DbConnection()
        {
            var dbName = "CustomersDB.db3";
            var path = Path.Combine(System.Environment.
                GetFolderPath(System.Environment.
                SpecialFolder.Personal), dbName);
            return new SQLiteConnection(path);
        }
    }
}

Open in new window


DatabaseConnection_IOS
using LocalDataAccess.iOS;
using SQLite;
using System;
using System.IO;
[assembly: Xamarin.Forms.Dependency(typeof(DatabaseConnection_iOS))]

namespace LocalDataAccess.iOS
{
    public class DatabaseConnection_iOS
    {
        public SQLiteConnection DBConnection()
        {
            var dbName = "CustomersDb.db3";
            string personalFolder =
                System.Environment.
                GetFolderPath(Environment.SpecialFolder.Personal);
            string libraryFolder =
                Path.Combine(personalFolder, "..", "Library");
            var path = Path.Combine(libraryFolder, dbName);
            return new SQLiteConnection(path);
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Thomas Swaney
Thomas Swaney
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I found the solution and the solution giving was not the fix for the problem.