FileNotFoundException Error creating Database WP8 Sqlite

290 views Asked by At

I am following this tutorial to create an application with an SQLite database.

When I executed it, I got System.IO.FileNotFoundException pointing to the file SQLite.cs, the line where the error originated is shown in the screen shot below(in the SQLite.cs file)

error line

Below is the code snippet for creating a database

string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");
if (!FileExists(dbPath).Result)
{
   using (var db = new SQLiteConnection(dbPath))
   {
        db.CreateTable<Person>();
   }
}

and the method FileExists

        private async Task<bool> FileExists(string fileName)
        {
            var result = false;
            try
            {
                var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
                result =true;
            }
            catch
            {
            }

            return result;

        }

I am wondering what went wrong. Any help?

1

There are 1 answers

0
Alberto Solano On BEST ANSWER

With the line if (!FileExists(dbPath).Result) you're saying that if the file doesn't exist, connect to the database and create a table. So your condition is wrong, because the line

Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName)

wants an existing file to execute correctly. Correct then your if condition with:

if (FileExists(dbPath).Result)