DapperLib/Dapper · error · ArgumentException

Cannot be null or empty

Error message

Cannot be null or empty

What it means

Thrown by the `SqlMapper.UdtTypeHandler` constructor when `udtTypeName` is null or empty. The handler needs a concrete UDT type name to set on `SqlDbType.Udt` parameters (SqlMapper.UdtTypeHandler.SetValue uses it as parameter.UdtTypeName); an empty name would produce an invalid provider call, so it is rejected at construction with ArgumentException.

Source

Thrown at Dapper/UdtTypeHandler.cs:21

namespace Dapper
{
    public static partial class SqlMapper
    {
        /// <summary>
        /// A type handler for data-types that are supported by the underlying provider, but which need
        /// a well-known UdtTypeName to be specified
        /// </summary>
        public class UdtTypeHandler : ITypeHandler
        {
            private readonly string udtTypeName;
            /// <summary>
            /// Creates a new instance of UdtTypeHandler with the specified <see cref="UdtTypeHandler"/>.
            /// </summary>
            /// <param name="udtTypeName">The user defined type name.</param>
            public UdtTypeHandler(string udtTypeName)
            {
                if (string.IsNullOrEmpty(udtTypeName)) throw new ArgumentException("Cannot be null or empty", udtTypeName);
                this.udtTypeName = udtTypeName;
            }

            object? ITypeHandler.Parse(Type destinationType, object value)
            {
                return value is DBNull ? null : value;
            }

            void ITypeHandler.SetValue(IDbDataParameter parameter, object value)
            {
#pragma warning disable 0618
                parameter.Value = SanitizeParameterValue(value);
#pragma warning restore 0618
                if(!(value is DBNull)) StructuredHelper.ConfigureUDT(parameter, udtTypeName);
            }
        }
    }
}

View on GitHub (pinned to 72a54c475f)

Solutions

  1. Supply the real UDT type name, e.g. `new SqlMapper.UdtTypeHandler("dbo.Geography")`.
  2. Validate/load the UDT name from configuration with a default and a clear error if missing.
  3. Trim the value and check it is non-empty before constructing the handler.
  4. If you do not need UDT handling, remove the handler registration.

Example fix

// before
SqlMapper.AddTypeHandler(typeof(Geo), new SqlMapper.UdtTypeHandler(config["UdtName"])); // config value missing -> null
// after
var udt = config["UdtName"] ?? throw new InvalidOperationException("UdtName not configured");
SqlMapper.AddTypeHandler(typeof(Geo), new SqlMapper.UdtTypeHandler(udt));
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(udtTypeName)) throw new ArgumentException("UDT name required", nameof(udtTypeName));
var handler = new SqlMapper.UdtTypeHandler(udtTypeName.Trim());

Type guard

static bool IsValidUdtName(string? s) => !string.IsNullOrWhiteSpace(s);

Prevention

When it happens

Trigger: Calling `new SqlMapper.UdtTypeHandler(null)` or `new SqlMapper.UdtTypeHandler("")`, or registering a type handler with a UDT name read from configuration that is missing/blank.

Common situations: Loading the UDT name from appsettings/environment and the key is absent; copy-pasting handler registration and forgetting the name; whitespace-only string (note: the check is IsNullOrEmpty, so a whitespace-only name passes but later fails at the provider).

Related errors


AI-assisted analysis of DapperLib/Dapper@72a54c475f (2026-08-13). Data as JSON: /api/errors/635c9d4792a7408b. Report an issue: GitHub.