classxamaringlobal-variablesavailability

Xamarin - Path to database must be defined


According to the tutorial I found, I declared the path for android to the db3 file in the MainActivity.cs like this:

        string fileName = "galleries.db3";
        string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        string completePath = Path.Combine(folderPath, fileName);

        LoadApplication(new App(completePath));

and in the App.xaml.cs I added this:

    public App(string filePath)
    {
        InitializeComponent();

        MainPage = new NavigationPage(new GalleryList());
        FilePath = filePath;
    }

I would like to access this path inside a class, like this:

    using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))

But get the error message that I have to specify a Path, and the variable is Null. How can I make this path available inside a class?


Solution

  • you are executing your db code (in GalleryList) before you assign FilePath

    MainPage = new NavigationPage(new GalleryList());
    FilePath = filePath;
    

    instead, assign FilePath first

    FilePath = filePath;
    MainPage = new NavigationPage(new GalleryList());