-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NullReferenceException thrown when DbString instance is null #1425
Comments
Do we have any plan to resolve this? |
Also having this issue |
I ran across this again using the latest version (v2.1.21) and found a fix (at least for my scenario). Original code that had a problem: var param = new {
sqlParam = maybeNullStr is null ? null : new DbString() { Value = maybeNullStr, Length = maybeNullStr.Length }
}; What I was tempted to do - which gets around the null reference exception var param = new {
sqlParam = new DbString() { Value = maybeNullStr, Length = maybeNullStr?.Length ?? 0 }
}; However if you're working with an Always Encrypted column like I was, you need to set the default size to the actual size of the sql column, otherwise you'll get an error like var param = new {
CardNumber = new DbString() { Value = maybeNullStr, Length = string.IsNullOrEmpty(maybeNullStr) ? 123 : maybeNullStr.Length }
};
eg: for a column like this: "CREATE TABLE CreditCards ([CardNumber] NVARCHAR(123) NULL) @LanceRomance you should just remove the question mark in (edited to work with null and empty strings) |
I'll see if we can fix this in the lib, sorry. I'm also looking to improve this in AOT: DapperLib/DapperAOT#89 |
Right, so I had a look at this, and I can't see the problem; can someone help me clarify the failing scenario? yes you do need to assign a non-null var args = new { x = new DbString() { Value = maybeNullStr, ... } }; Re:
Right; so set the correct size? Isn't that meant to be constant here? var args = new { x = new DbString() { Value = maybeNullStr, Size = 200 } }; where 200 is the size defined by the column I have looked at adding a .ctor (locally, not pushed yet): var args = new { x = new DbString(maybeNullStr, 200 } }; |
Trying to something sensible when the value is [Fact]
public void DbStringNullHandling()
{
// without lengths
var obj = new { x = new DbString("abc"), y = (DbString?)new DbString(null) };
var row = connection.QuerySingle<(string? x,string? y)>("select @x as x, @y as y", obj);
Assert.Equal("abc", row.x);
Assert.Null(row.y);
// with lengths
obj = new { x = new DbString("abc", 200), y = (DbString?)new DbString(null, 200) };
row = connection.QuerySingle<(string? x, string? y)>("select @x as x, @y as y", obj);
Assert.Equal("abc", row.x);
Assert.Null(row.y);
// null raw value - give clear message, at least
obj = obj with { y = null };
var ex = Assert.Throws<InvalidOperationException>(() => connection.QuerySingle<(string? x, string? y)>("select @x as x, @y as y", obj));
Assert.Equal("Member 'y' is an ICustomQueryParameter and cannot be null", ex.Message);
} The AOT work is probably the place to move any actual functionality enhancements; is there anything I'm still missing about the basic failure scenario here, that isn't addressed? |
I found when calling a stored procedure that has a DbString property and that instance is null, a NullReference exception is thrown.
Example:
I bumped into this due to code that looked something like:
I would expect the same behavior as passing a null string - a conversion to DbNull.Value.
The text was updated successfully, but these errors were encountered: