Confused null strings and DBNull

I am looking at the code for the .net program I am working on. The previous author of the program gave me the code more than a year ago. Suddenly I see an exception thrown from code that I did not touch:

if ((string)row["LevelName"] != "ABC") 

The exception is "Unable to pass an object of type" System.DBNull "to enter" System.String ".

I thought the string is a null data type, so how can I get this exception?

+4
source share
5 answers

DBNull is a native class that contains a singleton in the Value property. DBNull.Value is not null, as this is a reference to an instance of this class.

In most cases, if not all database wrappers return DBNull.Value instead of null when there is no value. One reason for this is that returning real null may mean that it does not have a row, not just a null value in the column (this depends on what objects you use to get the values).

As a rule, the as operator is very useful with DBNull and can be used with any type of NULL (including string ).

 string str = reader["Name"] as string; int? i = reader["Age"] as int?; 

It can also be noted that the operator ? also very useful here when you need a value type that is not nullable (although it doesnโ€™t look too pretty).

 int i = reader["Age"] as int? ?? -1; 

I find this very convenient as a small script anyway in the select LINQ clause.

+7
source

I believe what you are looking for:

 if ((row["LevelName"] as String) != "ABC") 

In DBNull and String there is no implicit push.

It may have worked before because there was simply no NULL in this column in your database. Some data may be corrupted, or someone has changed the NOT NULL constraint on a table.

Basically, if you are explicitly doing something, you must make sure that they have compatible dynamic types, otherwise an exception will occur. The as statement basically says: "Bring it to this, if possible, otherwise the boolean is null." Obviously, the as operator only works with reference types, since structs cannot be null.

+9
source

DBNull means that there is no data in this database column. This can happen if a new record is created in the database, but the column "LevelName" is not assigned a value.

DBNull is not the same as a reference to a null object or a null string variable. DBNull means that the column data has never been set. If you assign null to the column "LevelName", then the column has been initialized and it will return null (not DBNull).

+4
source

DBNull is a null value in the database. This is not the same as .NET null, which indicates a null reference.

The code suddenly crashes because someone changed or inserted a record with zeros in it.

Yes, the string is NULL, but you added the dbnull string to the string. You should check dbnull and replace it with null if it is there.

+3
source

There is a difference between .NET null and DBNull.Value . Here you can see what the DBNull and Value class are in this class.

You should do something like this:

 if (row["LevelName"].Value == DBNull.Value) return false; else if (row["LevelName"].ToString() != "ABC") // do something // .... 
+2
source

Source: https://habr.com/ru/post/1380085/


All Articles