Gitches
Gitches

Reputation: 81

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

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).

Upvotes: 6

Views: 3478

Answers (4)

ytez
ytez

Reputation: 71

It occurs when I specify the PublishSingleFile property to csproj in an application using System.Data.SQLite.Core. I solved it by setting the IncludeNativeLibrariesForSelfExtract property to true.

It's 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.

This 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):

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.

Upvotes: 7

Criper98
Criper98

Reputation: 61

There is a problem related to the PublishSingleFile setting and System.Data.SQLite.Core. In my case, changing IncludeNativeLibrariesForSelfExtract to true in .csproj doesn't solve the issue.

It is probably a bug in version 1.0.118 of System.Data.SQLite. Downgrading to 1.0.117 solved the issue for me.

Upvotes: 6

Ren&#233; Caspers
Ren&#233; Caspers

Reputation: 41

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.

Upvotes: 4

MIHOW
MIHOW

Reputation: 411

In my case what helped was to change PublishSingleFile to false.

Upvotes: 1

Related Questions