Difference Between Null and System.Dbnull.Value

What is the difference between null and System.DBNull.Value?

Well, null is not an instance of any type. Rather, it is an invalid reference.

However, System.DbNull.Value, is a valid reference to an instance of System.DbNull (System.DbNull is a singleton and System.DbNull.Value gives you a reference to the single instance of that class) that represents nonexistent* values in the database.

*We would normally say null, but I don't want to confound the issue.

So, there's a big conceptual difference between the two. The keyword null represents an invalid reference. The class System.DbNull represents a nonexistent value in a database field. In general, we should try avoid using the same thing (in this case null) to represent two very different concepts (in this case an invalid reference versus a nonexistent value in a database field).

Keep in mind, this is why a lot of people advocate using the null object pattern in general, which is exactly what System.DbNull is an example of.

Difference between nothing and system.DBNull

The keyword Nothing is used to specify or asign that a var of reference type is not pointing anything, no object is instanciated for this var.

DBNull.Value, on the other hand, is an object used to point out that a type of a field of the DataBase is of null value.

When we have to use DBNull.Value, null and in C#.Net?

null is one of two things:

  • a reference that doesn't actually point to an object - just a "nothing" indicator (essentially, it is the value 0 as a reference)
  • a Nullable<T> struct, which does not currently have a value (the HasValue property will also return false)

DBNull is specific to some parts of ADO.NET to represent null in the database. I have yet to think of a good reason why they didn't just use regular null here.

"" is a string literal with length zero - a perfectly valid, but empty, string. The significance of this is that between null string and a "" string, instance methods like value.Trim() will behave differently; null.Trim() will throw an exception; "".Trim() is just "". In general, using string.IsNullOrEmpty(value) as a test makes this distinction go away.

Real difference between DBNull.Value and just null?

yes when you are passing a datetime values especially you need to pass with DBNull.value. Because you are passing date time inside single quote if you place 'Null' like this it is treated as a string or varchar. In the same way if you pass DBNull.value means this value is not passed so sql will treat it as null.

Please visit this link. here they explained with example. This may help you.

http://onlydifferencefaqs.blogspot.in/2012/09/null-keyword-vs-dbnull-class-in-c.html

Difference between DbNull.Value and DbNull.Value.ToString()


cmd.Parameters.Add(new SqlParameter("@ParentSesID", parentID));

This is passing a parentID to parameter @ParentSesID.

cmd.Parameters.Add(new SqlParameter("@ParentSesID", DBNull.Value));

is passing a null value to parameter.

cmd.Parameters.Add(new SqlParameter("@ParentSesID", DBNull.Value.ToString()));

is passing equal to string.Empty, which is not allowed in numerical data types.

cmd.Parameters.Add(new SqlParameter("@ParentSesID", null);

is same as ignoring the parameter.

So when you need to pass null to SP you've to pass DBNull.Value.

(any == System.DBNull.Value) vs (any is System.DBNull)


if (any == System.DBNull.Value) ...

I prefer that one, simply because I read that as comparing values, not types.

Simplest way to check for DBNull and null at once

You can use the ?? operator with nullables.

return (value as int?) ?? 0;

If you're feeling bold, you can even take out the parens.

What is the point of DBNull?

The point is that in certain situations there is a difference between a database value being null and a .NET Null.

For example. If you using ExecuteScalar (which returns the first column of the first row in the result set) and you get a null back that means that the SQL executed did not return any values. If you get DBNull back it means a value was returned by the SQL and it was NULL. You need to be able to tell the difference.



Related Topics



Leave a reply



Submit