aspnetboilerplate/aspnetboilerplate · error · ArgumentNullException

cannot be null or empty.

Error message

{nameof(sql)} cannot be null or empty.

What it means

SqlDialectBase.GetCountSql wraps a SQL fragment in a SELECT COUNT(*) query, but requires the inner sql string to be non-empty and non-whitespace. When null or empty is passed, it throws ArgumentNullException naming the 'sql' parameter. The message renders the literal string '{nameof(sql)}' because the interpolated nameof expression was written inside a quoted string rather than being evaluated.

Solutions

  1. Ensure the sql string passed to GetCountSql is a non-empty, non-whitespace SQL fragment before calling
  2. Guard the caller: check string.IsNullOrWhiteSpace(sql) upstream and handle it instead of calling GetCountSql
  3. If this is your own dialect override, validate inputs before calling base.GetCountSql

Example fix

// before
var countSql = dialect.GetCountSql(sqlFragment);
// after
var countSql = string.IsNullOrWhiteSpace(sqlFragment)
    ? null
    : dialect.GetCountSql(sqlFragment);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(sql)) throw new ArgumentException("A non-empty SQL fragment is required before calling GetCountSql.", nameof(sql));

Try / catch

try { countSql = dialect.GetCountSql(sql); } catch (ArgumentNullException ex) when (ex.ParamName == "sql") { /* fall back to manual count query or rethrow with context */ }

Prevention

When it happens

Trigger: Calling GetCountSql(null) or GetCountSql("") or GetCountSql(" ") on an ISqlDialect instance, directly or through code that builds a count query from an uninitialized SQL fragment.

Common situations: Building count queries dynamically where the base SQL string variable was never assigned or was trimmed to empty; overriding dialect methods and calling base.GetCountSql with a derived value that can be null.

Related errors


AI-assisted analysis of aspnetboilerplate/aspnetboilerplate@2323c13a15 (2026-09-08). Data as JSON: /api/errors/de4142c0b9de2ed0. Report an issue: GitHub.

Appendix: source

Thrown at src/Abp.Dapper/Dapper-Extensions/Sql/SqlDialectBase.cs:156

        {
            return value;
        }
        return string.Format("{0}{1}{2}", OpenQuote, value.Trim(), CloseQuote);
    }

    public virtual string UnQuoteString(string value)
    {
        return IsQuoted(value) ? value.Substring(1, value.Length - 2) : value;
    }

    public abstract string GetDatabaseFunctionString(DatabaseFunction databaseFunction, string columnName, string functionParameters = "");

    public abstract void EnableCaseInsensitive(IDbConnection connection);

    public virtual string GetCountSql(string sql)
    {
        if (string.IsNullOrEmpty(sql))
            throw new ArgumentNullException(nameof(sql), $"{nameof(sql)} cannot be null or empty.");

        if (string.IsNullOrWhiteSpace(sql))
            throw new ArgumentNullException(nameof(sql), $"{nameof(sql)} cannot be null or empty.");

        return $"SELECT COUNT(*) AS {OpenQuote}Total{CloseQuote} FROM {sql}";
    }

    protected virtual bool IsSelectSql(string sql)
    {
        return sql.Trim().StartsWith("SELECT", StringComparison.InvariantCultureIgnoreCase);
    }
}

View on GitHub (pinned to 2323c13a15)