1

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.

Community
  • 1
  • 1
PeterB
  • 313
  • 4
  • 19
  • Your code is Xamarin Android code, but the example and question is Xamarin PCL project. Can you confirm which kind of project you are using? Xamarin PCL or Xamarin Andorid? – Mike Ma Mar 27 '17 at 07:39

2 Answers2

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" />
Yoruba
  • 2,572
  • 22
  • 26
0

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

guest
  • 1
  • This should have been shared more as a comment on the issue rather than an answer to ease the task of other users finding the solution. – stanley mbote May 11 '22 at 11:18
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 11 '22 at 12:00
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31743297) – JonC May 16 '22 at 12:53