Null Strikes Again! (even with C# special operators)
-
I believe you'll find this interesting and a possible discussion. But, also, I ain't too smart so I may just not be seeing something. I'm using the cutting edge latest version of C# 10 (in .NET Core 6.0.2 web api I'm building). I'm saving data to a sqlite database & I have some code where I want to allow the value to be null (inserted into the db) if hte user doesn't supply that data.
sqliteProvider.command.Parameters.AddWithValue("$screenName", task.screenName);
task.screenName
is a nullable string. Null This, Null That -- There are different kinds of null However, if the value is actually a null string because the user has opted to not provide the value then I need to actually insert a System.DbNull.Value into the databse -- that's a true DBNull -- not the String null which the db chokes on. The New Null Operators So, I'm new & I'm hip to the scene of these null coalescing (??= ??), Elvis operators (?: ) and all that stuff, you dig man? But it's a no-go. Null Still Ate My Code Here's what I wanted. I wanted to call theAddWithValue()
with the String value when the nullable string (task.screenName) isn't null... ....and I wanted to callAddWithValue()
with a System.DbNull.Value (so null would be properly passed and inserted into the db) when the nullable String is null. I was hoping for something like:sqliteProvider.command.Parameters.AddWithValue("$screenName", task.screenName ?? System.DbNull.Value);
I was hoping it would say, "if the String is null, then use the System.DBNull.Value. Two Different Types But, like, far out and way out man. It ain't happening, because those are two different types. And, dig it, to get these null operators to work, they have to be the same type. So I had to write a method:
private Object convertDbNull(String? s){
if (s == null){
return System.DBNull.Value;
}
return s;
}Then call my code like this:
sqliteProvider.command.Parameters.AddWithValue("$screenName",convertDbNull(task.ScreenName));
It'll return the valid String (when not null) or it'll return the System.DBNull.Value (when the string is null). Surely, I'm Missing Some Super Special Null Operator?? If you have a better way where I can use one of the new null operators, please, please lay it on me. :laugh: So the Multi-Billion-dollar Null Problem still exists, appa
-
I believe you'll find this interesting and a possible discussion. But, also, I ain't too smart so I may just not be seeing something. I'm using the cutting edge latest version of C# 10 (in .NET Core 6.0.2 web api I'm building). I'm saving data to a sqlite database & I have some code where I want to allow the value to be null (inserted into the db) if hte user doesn't supply that data.
sqliteProvider.command.Parameters.AddWithValue("$screenName", task.screenName);
task.screenName
is a nullable string. Null This, Null That -- There are different kinds of null However, if the value is actually a null string because the user has opted to not provide the value then I need to actually insert a System.DbNull.Value into the databse -- that's a true DBNull -- not the String null which the db chokes on. The New Null Operators So, I'm new & I'm hip to the scene of these null coalescing (??= ??), Elvis operators (?: ) and all that stuff, you dig man? But it's a no-go. Null Still Ate My Code Here's what I wanted. I wanted to call theAddWithValue()
with the String value when the nullable string (task.screenName) isn't null... ....and I wanted to callAddWithValue()
with a System.DbNull.Value (so null would be properly passed and inserted into the db) when the nullable String is null. I was hoping for something like:sqliteProvider.command.Parameters.AddWithValue("$screenName", task.screenName ?? System.DbNull.Value);
I was hoping it would say, "if the String is null, then use the System.DBNull.Value. Two Different Types But, like, far out and way out man. It ain't happening, because those are two different types. And, dig it, to get these null operators to work, they have to be the same type. So I had to write a method:
private Object convertDbNull(String? s){
if (s == null){
return System.DBNull.Value;
}
return s;
}Then call my code like this:
sqliteProvider.command.Parameters.AddWithValue("$screenName",convertDbNull(task.ScreenName));
It'll return the valid String (when not null) or it'll return the System.DBNull.Value (when the string is null). Surely, I'm Missing Some Super Special Null Operator?? If you have a better way where I can use one of the new null operators, please, please lay it on me. :laugh: So the Multi-Billion-dollar Null Problem still exists, appa
sqliteProvider.command.Parameters.AddWithValue("$screenName", (object) task.screenName ?? System.DbNull.Value);
That should work. C# won't coerce a type (even if it's valid) to a type not present in the expression. Here it sees
string
andDBNull
, so even thoughobject
is a valid base for them,object
isn't available unless you explicitly opt-in. That's my understanding at least :thumbsup: EDIT: You might need a ? for the cast. Unsure since I'm still on an older version of C# so I haven't had to deal with that new stuff yet. -
sqliteProvider.command.Parameters.AddWithValue("$screenName", (object) task.screenName ?? System.DbNull.Value);
That should work. C# won't coerce a type (even if it's valid) to a type not present in the expression. Here it sees
string
andDBNull
, so even thoughobject
is a valid base for them,object
isn't available unless you explicitly opt-in. That's my understanding at least :thumbsup: EDIT: You might need a ? for the cast. Unsure since I'm still on an older version of C# so I haven't had to deal with that new stuff yet. -
sqliteProvider.command.Parameters.AddWithValue("$screenName", (object) task.screenName ?? System.DbNull.Value);
That should work. C# won't coerce a type (even if it's valid) to a type not present in the expression. Here it sees
string
andDBNull
, so even thoughobject
is a valid base for them,object
isn't available unless you explicitly opt-in. That's my understanding at least :thumbsup: EDIT: You might need a ? for the cast. Unsure since I'm still on an older version of C# so I haven't had to deal with that new stuff yet.Oh my gosh!! I was so close!!! That works -- well it compiles anyways. I will test to insure that it definitely does what I expect, but it looks good:
sqliteProvider.command.Parameters.AddWithValue("$screenName",(object)task.ScreenName ?? System.DBNull.Value);
Wow! confirmed!! I built it and ran it and it works. I'm so glad I ranted. :laugh: Thanks again.
-
I believe you'll find this interesting and a possible discussion. But, also, I ain't too smart so I may just not be seeing something. I'm using the cutting edge latest version of C# 10 (in .NET Core 6.0.2 web api I'm building). I'm saving data to a sqlite database & I have some code where I want to allow the value to be null (inserted into the db) if hte user doesn't supply that data.
sqliteProvider.command.Parameters.AddWithValue("$screenName", task.screenName);
task.screenName
is a nullable string. Null This, Null That -- There are different kinds of null However, if the value is actually a null string because the user has opted to not provide the value then I need to actually insert a System.DbNull.Value into the databse -- that's a true DBNull -- not the String null which the db chokes on. The New Null Operators So, I'm new & I'm hip to the scene of these null coalescing (??= ??), Elvis operators (?: ) and all that stuff, you dig man? But it's a no-go. Null Still Ate My Code Here's what I wanted. I wanted to call theAddWithValue()
with the String value when the nullable string (task.screenName) isn't null... ....and I wanted to callAddWithValue()
with a System.DbNull.Value (so null would be properly passed and inserted into the db) when the nullable String is null. I was hoping for something like:sqliteProvider.command.Parameters.AddWithValue("$screenName", task.screenName ?? System.DbNull.Value);
I was hoping it would say, "if the String is null, then use the System.DBNull.Value. Two Different Types But, like, far out and way out man. It ain't happening, because those are two different types. And, dig it, to get these null operators to work, they have to be the same type. So I had to write a method:
private Object convertDbNull(String? s){
if (s == null){
return System.DBNull.Value;
}
return s;
}Then call my code like this:
sqliteProvider.command.Parameters.AddWithValue("$screenName",convertDbNull(task.ScreenName));
It'll return the valid String (when not null) or it'll return the System.DBNull.Value (when the string is null). Surely, I'm Missing Some Super Special Null Operator?? If you have a better way where I can use one of the new null operators, please, please lay it on me. :laugh: So the Multi-Billion-dollar Null Problem still exists, appa
That's an API problem, not really related to null or not null, but the fact that .NET DB drivers assume "null" as unset, and DBNull.Value as real null (WPF did it a little better, as we have Unset as a separate item, and null really means null). You can probably create a helper/extension method that converts null to DBNull.Value and it should work fine. I also consider it very bad that IDataParameter always box value types. It shouldn't need to do it, as I can clearly tell by working on C++ drivers that primitive types can just be primitive types and never "boxed" or similar there.
-
I believe you'll find this interesting and a possible discussion. But, also, I ain't too smart so I may just not be seeing something. I'm using the cutting edge latest version of C# 10 (in .NET Core 6.0.2 web api I'm building). I'm saving data to a sqlite database & I have some code where I want to allow the value to be null (inserted into the db) if hte user doesn't supply that data.
sqliteProvider.command.Parameters.AddWithValue("$screenName", task.screenName);
task.screenName
is a nullable string. Null This, Null That -- There are different kinds of null However, if the value is actually a null string because the user has opted to not provide the value then I need to actually insert a System.DbNull.Value into the databse -- that's a true DBNull -- not the String null which the db chokes on. The New Null Operators So, I'm new & I'm hip to the scene of these null coalescing (??= ??), Elvis operators (?: ) and all that stuff, you dig man? But it's a no-go. Null Still Ate My Code Here's what I wanted. I wanted to call theAddWithValue()
with the String value when the nullable string (task.screenName) isn't null... ....and I wanted to callAddWithValue()
with a System.DbNull.Value (so null would be properly passed and inserted into the db) when the nullable String is null. I was hoping for something like:sqliteProvider.command.Parameters.AddWithValue("$screenName", task.screenName ?? System.DbNull.Value);
I was hoping it would say, "if the String is null, then use the System.DBNull.Value. Two Different Types But, like, far out and way out man. It ain't happening, because those are two different types. And, dig it, to get these null operators to work, they have to be the same type. So I had to write a method:
private Object convertDbNull(String? s){
if (s == null){
return System.DBNull.Value;
}
return s;
}Then call my code like this:
sqliteProvider.command.Parameters.AddWithValue("$screenName",convertDbNull(task.ScreenName));
It'll return the valid String (when not null) or it'll return the System.DBNull.Value (when the string is null). Surely, I'm Missing Some Super Special Null Operator?? If you have a better way where I can use one of the new null operators, please, please lay it on me. :laugh: So the Multi-Billion-dollar Null Problem still exists, appa
Null reference type are advisory at best, they are not enforced, and there are plenty of way to make them fails: [Nullable reference types | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references)
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
I believe you'll find this interesting and a possible discussion. But, also, I ain't too smart so I may just not be seeing something. I'm using the cutting edge latest version of C# 10 (in .NET Core 6.0.2 web api I'm building). I'm saving data to a sqlite database & I have some code where I want to allow the value to be null (inserted into the db) if hte user doesn't supply that data.
sqliteProvider.command.Parameters.AddWithValue("$screenName", task.screenName);
task.screenName
is a nullable string. Null This, Null That -- There are different kinds of null However, if the value is actually a null string because the user has opted to not provide the value then I need to actually insert a System.DbNull.Value into the databse -- that's a true DBNull -- not the String null which the db chokes on. The New Null Operators So, I'm new & I'm hip to the scene of these null coalescing (??= ??), Elvis operators (?: ) and all that stuff, you dig man? But it's a no-go. Null Still Ate My Code Here's what I wanted. I wanted to call theAddWithValue()
with the String value when the nullable string (task.screenName) isn't null... ....and I wanted to callAddWithValue()
with a System.DbNull.Value (so null would be properly passed and inserted into the db) when the nullable String is null. I was hoping for something like:sqliteProvider.command.Parameters.AddWithValue("$screenName", task.screenName ?? System.DbNull.Value);
I was hoping it would say, "if the String is null, then use the System.DBNull.Value. Two Different Types But, like, far out and way out man. It ain't happening, because those are two different types. And, dig it, to get these null operators to work, they have to be the same type. So I had to write a method:
private Object convertDbNull(String? s){
if (s == null){
return System.DBNull.Value;
}
return s;
}Then call my code like this:
sqliteProvider.command.Parameters.AddWithValue("$screenName",convertDbNull(task.ScreenName));
It'll return the valid String (when not null) or it'll return the System.DBNull.Value (when the string is null). Surely, I'm Missing Some Super Special Null Operator?? If you have a better way where I can use one of the new null operators, please, please lay it on me. :laugh: So the Multi-Billion-dollar Null Problem still exists, appa
raddevus wrote:
I need to actually insert a System.DbNull.Value into the databse -- that's a true DBNull -- not the String null which the db chokes on.
That statement makes no sense. Given a string/text value in a database it can have one of the following values. 1. Null 2. Empty string. Some databases do not allow this 3. Any other non-empty value. That list does not include 'DBNull'. The 'DBNull' value is usually a value that is intended as a stand in for results for the first item in the list above. Perhaps your real problem is that you need to represent the following 1. A value that is not null 2. User did not 'choose' anything. So you need to represent 'no value', but you cannot use 'null'. In the above you then use a magical value. If your database supports it and there NO chance it will be needed then an empty string can be used. But otherwise some nonsensical value is chosen. For example say you need to provide a customer telephone number. So you allow numbers. But for your special value you allow 'xxx-xxx-xxxx'. Naturally you must special case the code. HOWEVER, this is not an idea solution. And you should first examine the requirements that state that 'null' cannot be used in the first place. That the database does not allow it is NOT a argument for that. Because that to is just code and it must meet the needs of the business and not arbitrary developer opinions.