0

I am using this tutorial to show sqlite data in my app. Use a local database in Xamarin but it did not work for me. help me with my errors in code. Error shown on Visual Studio is "an unhandled exception occur", and app fails to open on my phone. i am not using any emulator. thanks

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

using System.Data;
using System.IO;
using SQLite;

namespace DicApp
{
public class Album
{
    [PrimaryKey, AutoIncrement]
    public int AlbumId { get; set; }
    public string Title { get; set; }
    public int ArtistId { get; set; }
}

[Activity(Label = "DicApp", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        string dbName = "db.sqlite";
        string dbPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(), dbName);
        // Check if your DB has already been extracted.
        if (!File.Exists(dbPath))
        {
            using (BinaryReader br = new BinaryReader(Assets.Open(dbName)))
            {
                using (BinaryWriter bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create)))
                {
                    byte[] buffer = new byte[2048];
                    int len = 0;
                    while ((len = br.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        bw.Write(buffer, 0, len);
                    }
                }
            }
        }

        using (var conn = new SQLite.SQLiteConnection(dbPath))
        {
            var cmd = new SQLite.SQLiteCommand (conn);
cmd.CommandText = "select * from Album";
var r = cmd.ExecuteQuery<Album> ();

Console.Write (r);
        }
    }

}
}
Community
  • 1
  • 1
Zeeshan
  • 43
  • 1
  • 13
  • You need to figure out what line is causing the exception, and what the specific exception is. Also check that you have the appropriate permissions set for file access in your manifest. – Jason Jul 01 '15 at 13:01
  • all permissions are set. there is nothing showing the line about the error. how can i see the error line. – Zeeshan Jul 03 '15 at 07:29
  • wrap your code in a try/catch, or selectively comment out sections until you determine which is causing the problem. – Jason Jul 03 '15 at 13:22

0 Answers0