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