Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Local database not working on Windows Phone

$
0
0

Hello!
I am developing an XForms app with a PCL. It uses a local database. I followed all the steps in this tutorial: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/databases/ .

Now my app is working on iOs and Android but not on Windows Phone.
This is my code:

ISQLite.cs (in PCL):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SQLite.Net;

namespace MyApp
{
    public interface ISQLite
    {
        SQLiteConnection GetConnection();
    }
}

SQLite_WinPhone.cs

using System;
using System.IO;
using System.IO.IsolatedStorage;
using MyApp;
using MyApp.WinPhone;
using Xamarin.Forms;
using Windows.Storage;

[assembly: Dependency(typeof(SQLite_WinPhone))]

namespace MyApp.WinPhone
{
    public class SQLite_WinPhone : ISQLite
    {
        public SQLite_WinPhone()
        {
        }

        #region ISQLite implementation
        public SQLite.Net.SQLiteConnection GetConnection()
        {
            var sqliteFilename = "MyDB.db3";
            string path = Path.Combine(ApplicationData.Current.LocalFolder.Path, sqliteFilename);

            var storageFile = IsolatedStorageFile.GetUserStoreForApplication();

            if (!storageFile.FileExists(path))
            {
                using (var resourceStream = System.Windows.Application.GetResourceStream(new Uri(sqliteFilename, UriKind.Relative)).Stream)
                {
                    using (var fileStream = storageFile.CreateFile(path))
                    {
                        byte[] readBuffer = new byte[4096];
                        int bytes = -1;

                        while ((bytes = resourceStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
                        {
                            fileStream.Write(readBuffer, 0, bytes);
                        }
                    }
                }
            }

            var plat = new SQLite.Net.Platform.WindowsPhone8.SQLitePlatformWP8();
            var conn = new SQLite.Net.SQLiteConnection(plat, path);

            // Return the database connection 
            return conn;
        }
        #endregion
    }
}

As soon as the app access the database I get an error on this line var conn = new SQLite.Net.SQLiteConnection(plat, path);

This is the error:
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.ni.dll
'TaskHost.exe' (CoreCLR: Silverlight AppDomain): Loaded 'C:\windows\system32\System.Runtime.Serialization.ni.dll'. Symbols loaded.
'TaskHost.exe' (CoreCLR: Silverlight AppDomain): Loaded 'C:\windows\system32\System.Dynamic.Runtime.ni.dll'. Symbols loaded.
'TaskHost.exe' (CoreCLR: Silverlight AppDomain): Loaded 'C:\Data\Programs{2BDEA965-0F14-420F-8AF7-655301779EFE}\Install\SQLite.Net.Platform.WindowsPhone8.DLL'. Cannot find or open the PDB file.
The thread 0xf10 has exited with code 259 (0x103).
'TaskHost.exe' (CoreCLR: Silverlight AppDomain): Loaded 'C:\windows\system32\System.Text.Encoding.ni.dll'. Symbols loaded.
'TaskHost.exe' (CoreCLR: Silverlight AppDomain): Loaded 'C:\Data\Programs{2BDEA965-0F14-420F-8AF7-655301779EFE}\Install\Sqlite.winmd'. Module was built without symbols.
A first chance exception of type 'System.IO.FileNotFoundException' occurred in SQLite.Net.Platform.WindowsPhone8.DLL
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.ni.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in Microsoft.Phone.ni.dll
The program '[1704] TaskHost.exe' has exited with code -1 (0xffffffff).

The file MyDB.db3 is in the main folder of the Windows Phone project. with Build Action = Content (I even tried with Embedded Resource).

How is this possible?

Thanks!


Viewing all articles
Browse latest Browse all 58056

Trending Articles