litedb-org/LiteDB · error · ArgumentNullException

connectionString

Error message

connectionString

What it means

ArgumentNullException thrown by the ConnectionString(string) constructor when the connectionString argument is null or an empty string. The constructor parses a key=value;key=value format or treats a bare string as a filename, so a null/empty input has nothing to parse.

Source

Thrown at LiteDB/Client/Structures/ConnectionString.cs:70

        /// "collation": Set default collaction when database creation (default: "[CurrentCulture]/IgnoreCase")
        /// </summary>
        public Collation Collation { get; set; }

        /// <summary>
        /// Initialize empty connection string
        /// </summary>
        public ConnectionString()
        {
            _values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
        }

        /// <summary>
        /// Initialize connection string parsing string in "key1=value1;key2=value2;...." format or only "filename" as default (when no ; char found)
        /// </summary>
        public ConnectionString(string connectionString)
            : this()
        {
            if (string.IsNullOrEmpty(connectionString)) throw new ArgumentNullException(nameof(connectionString));

            // create a dictionary from string name=value collection
            if (connectionString.Contains("="))
            {
                _values.ParseKeyValue(connectionString);
            }
            else
            {
                _values["filename"] = connectionString;
            }

            // setting values to properties
            this.Connection = _values.GetValue("connection", this.Connection);
            this.Filename = _values.GetValue("filename", this.Filename).Trim();

            this.Password = _values.GetValue("password", this.Password);

            if(this.Password == string.Empty)

View on GitHub (pinned to f906a5f850)

Solutions

  1. Provide a valid filename or key=value pair string.
  2. Read the connection string from configuration with a fallback default.
  3. Validate the configuration value is non-empty before constructing.

Example fix

// before
var cs = new ConnectionString(config["LiteDB:ConnectionString"]); // may be null
// after
var connStr = config["LiteDB:ConnectionString"] ?? "MyData.db";
var cs = new ConnectionString(connStr);
Defensive patterns

Strategy: validation

Validate before calling

var connStr = configuration ?? "MyData.db";
if (string.IsNullOrEmpty(connStr))
    throw new InvalidOperationException("Connection string is not configured.");
var cs = new ConnectionString(connStr);

Type guard

static bool IsValidConnectionString(string s) => !string.IsNullOrEmpty(s);

Try / catch

try
{
    var cs = new ConnectionString(rawConnStr);
}
catch (ArgumentNullException ex) when (ex.ParamName == "connectionString")
{
    rawConnStr = "MyData.db"; // fallback
    var cs = new ConnectionString(rawConnStr);
}

Prevention

When it happens

Trigger: Calling new ConnectionString(null), new ConnectionString(""), or passing a variable that resolved to null/empty (e.g., from configuration, command-line args, or environment variables that were not set).

Common situations: Reading the connection string from appsettings.json where the key is missing. Using environment variables that were not set in the deployment. Passing a null default when no configuration is available. Dependency injection resolving a null string into the constructor.

Related errors


AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13). Data as JSON: /api/errors/5eb549ed4267fd4c. Report an issue: GitHub.