Search code examples
c#sqlnulllocaldb

Value cannot be null. Parameter name: path


I'm working with a local dataBase in a Windows Form Application and I'm trying to implement the source for the dataBase as DataDirectory, so in case I'm moving the db from one computer to another, it will work just fine. I wrote the following code, but I get this error, that the value cannot be null, at the line where it gets the Fullpath. Thanks !

var dataDirectory = ConfigurationManager.AppSettings["DataDirectory"];
var absoluteDataDirectory = Path.GetFullPath(dataDirectory);
AppDomain.CurrentDomain.SetData("DataDirectory", absoluteDataDirectory);
var connString = (@"Data Source= |DataDirectory|\Angajati.sdf");

Solution

  • You should have this section in your app.config:

    <appSettings>
        <add key="DataDirectory" value="DataDirectoryPath"/>
    </appSettings>
    

    By the way for accessing DataDirectory you have to use this code:

    AppDomain.CurrentDomain.GetData("DataDirectory")
    

    more info.