"Value cannot be null. (Parameter 'path1')" on connectionString

1.6k Views Asked by At

When debugging I can make a successful connection to an SQLite database. But after building the .NET application SQLite has trouble using system.IO.Path.Combine:

at System.IO.Path.Combine(String path1, String path2)
at System.Data.SQLite.SQLiteConnection..ctor(String connectionString, Boolean parseViaFramework)
at LDF_DetectionTool.DatabaseConnector.GetApplicationsList() in SomePath\DatabaseConnector.cs:line 23

The code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.Data;

namespace LDF_DetectionTool
{
    internal class DatabaseConnector
    {

        public List<string> GetApplicationsList()
        {
            string databaseFileName = "Databases\\LDF_DETECTION_TOOL_DATA.db";
            string databaseFilePath = AppDomain.CurrentDomain.BaseDirectory + databaseFileName;
            string connectionString = "Data Source=" + databaseFilePath;

            List<string> applicationList = new List<string>();
            try
            {
                using (SQLiteConnection connection = new SQLiteConnection(connectionString, true))
                {
                    connection.Open();

                    string query = "SELECT * FROM APPLICATIONS";
                    using (SQLiteCommand command = new SQLiteCommand(query, connection))
                    {
                        using (SQLiteDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                applicationList.Add(reader.GetString(0));
                            }
                        }
                    }

                    connection.Close();
                }
            } 
            catch (Exception ex) 
            {
                MessageBox.Show(connectionString);
                MessageBox.Show(ex.Message);
                MessageBox.Show(ex.StackTrace);
            }

            return applicationList;
        }

The exception message :

Value cannot be null. (Parameter 'path1')

None of the variables are null (I can display them in messageboxes, even after building). There is something wrong after build which works when debugging. The database is in the right location.

I tried reinstalling the nuget package, removing my own use of Path.Combine (already gone in above code), rebuilding several times, reboot Visual Studio and build again and setting parseViaFramework to true and false (new SQLiteConnection(connectionString, false) on line 23).

4

There are 4 best solutions below

0
René Caspers On

I had the same problem few weeks early.

It looks for Assembly.GetExecutingAssembly().Location.

My code was in a single file windows service so this location is an empty string.

In debug it was not empty.

Hope this helps.

0
ytez On

I ran into the same problem.

It appears to occur when I specify the PublishSingleFile property to csproj in an application using System.Data.SQLite.Core.

As a result, I solved it by setting the IncludeNativeLibrariesForSelfExtract property to true.

As René pointed out, it seems to be an issue with Assembly.GetExecutingAssembly().Location. In the single executable publish format, the current path of the assembly cannot be retrieved, because the managed DLLs are extracted and loaded in memory.

The following page says that by setting the IncludeNativeLibrariesForSelfExtract property to true, all files including managed assemblies will be extracted to a folder (probably a temporary folder).

https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file/overview?tabs=cli

Only managed DLLs are bundled with the app into a single executable. When the app starts, the managed DLLs are extracted and loaded in memory, avoiding the extraction to a folder. With this approach, the managed binaries are embedded in the single file bundle, but the native binaries of the core runtime itself are separate files.

To embed those files for extraction and get one output file, set the property IncludeNativeLibrariesForSelfExtract to true.

Specifying IncludeAllContentForSelfExtract extracts all files, including the managed assemblies, before running the executable. This may be helpful for rare application compatibility problems.

0
MIHOW On

I had the same problem, tried solution with IncludeNativeLibrariesForSelfExtract but didn't help in my case. What helped, though was to change PublishSingleFile to false.

1
Criper98 On

As @ytez already pointed out, there is a problem related to the PublishSingleFile setting and System.Data.SQLite.Core.

In my case, adding IncludeNativeLibrariesForSelfExtract true in .csproj doesn't solve the issue.

But i figured out that it is probably a bug isolated to the version 1.0.118 of the System.Data.SQLite package; in fact, downgrading the version to the previous release 1.0.117 solved the issue (for me).