Search code examples
c#wpfoledb

Application current path c# wpf


I'm trying to point to my database, the application is working, but I'm getting an error in the xaml binding.

            con = new OleDbConnection
        {
            ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "Database\\Baza_Info.accdb"
        };

It's working fine, but I'm getting error. This code is in a usercontrol. On another wpf window I got this:

    <Window.DataContext>
    <ViewModels:AdminViewModels/>
</Window.DataContext>

It throws the error here, and it says the path to my visual studio installation Common7/IDE/Database is not valid. Select a path that's spelled correctly blabla connected to the server.

help please.


Solution

  • If your data file is under the same folder as the executable then use "|DataDirectory|" for the folder path.

    con = new OleDbConnection
    {
        ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=|DataDirectory|\Database\Baza_Info.accdb"
    };