0

When i add the Sql-net-pcl library in my xamarin app, and compile, then it showing me an exception and saying that your application is in break mode.

Exception :

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

ISqLiteDb :

public interface ISqLiteDb
{
    SQLiteAsyncConnection GetConnection();
}

SqLiteDb :

class SqLiteDb: ISqLiteDb
{
    public SQLiteAsyncConnection GetConnection()
    {
        var documePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        var path = Path.Combine(documePath, "Shopping.db");

        return new SQLiteAsyncConnection(path);
    }
}

And here i am opening the connection and creating the table.

MainPage.Xaml.cs :

public partial class MainPage : ContentPage
{
    private ObservableCollection<Recipe> _recipes;

    private readonly SQLiteAsyncConnection _connection;
    public MainPage()
    {
        InitializeComponent();
        _connection = DependencyService.Get<ISqLiteDb>().GetConnection();
    }

    protected override async void OnAppearing()
    {
        await _connection.CreateTableAsync<Recipe>();
        var recipes = await _connection.Table<Recipe>().ToListAsync();

        _recipes = new ObservableCollection<Recipe>(recipes);

        listView.ItemsSource = _recipes;
    }

    private async void OnAdd(object sender, EventArgs e)
    {
        var recipe = new Recipe
        {
            Id = 1,
            Name = "Recipe1"
        };

        await _connection.InsertAsync(recipe);
        _recipes.Add(recipe);
    }
Malik Haseeb
  • 471
  • 9
  • 20

0 Answers0