'System.TypeInitializationException: The type initializer for 'SQLite.SQLiteConnection' threw an exception

I'm new to android and xamarin. Recently I have created one android app following xamrarin sample TaskyPortable. Problem is whenever following code executes it throws error.

        public override void OnCreate()
        {
            base.OnCreate();
            var sqliteFilename = "ToDoItemDB.db3";
            string libraryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            var path = Path.Combine(libraryPath, sqliteFilename);
            conn = new SQLiteConnection(path);

whenever it calls new SQLiteConnection with given path, it throws

"System.TypeInitializationException: The type initializer for 'SQLite.SQLiteConnection' threw an exception."

What am I doing wrong? The code is almost similar to TaskyPortable. I have searched a lot but did not find anything.

Kindly help.

P.S. : This question is already asked here and I have checked the answer. I have added reference of SQLite.net in droid project also. But still facing this issue.



Solution 1:[1]

This code is working

  private SQLiteConnection conn;      

  protected override void OnCreate(Bundle bundle)
  {
   base.OnCreate(bundle);
   Forms.Init(this, bundle);

   var sqliteFilename = "ToDoItemDB.db3";
   string libraryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
   var path = Path.Combine(libraryPath, sqliteFilename);
   conn = new SQLiteConnection(path);

   //create table
   conn.CreateTable<Todo>();

   LoadApplication(new App());
  }

Check if those packages are in the package.config. Maybe is better to remove the Sqlite packages and install only the sqlite-net-pcl, which is installing the rest of the sqlite packages

  <package id="sqlite-net-pcl" version="1.3.1" targetFramework="monoandroid60" />
  <package id="SQLitePCLRaw.bundle_green" version="1.1.2" targetFramework="monoandroid60" />
  <package id="SQLitePCLRaw.core" version="1.1.2" targetFramework="monoandroid60" />
  <package id="SQLitePCLRaw.lib.e_sqlite3.android" version="1.1.2" targetFramework="monoandroid60" />
  <package id="SQLitePCLRaw.provider.e_sqlite3.android" version="1.1.2" targetFramework="monoandroid60" />

Solution 2:[2]

I had encountered same error. In my case, mysql.Data package crashed. After delete mysql.Data package it solved.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yoruba
Solution 2 guest