Questions tagged [dbnull]

DBNull is a special value in The .Net framework indicating that the value is NULL in the database.

DBNull is a special value in the .Net framework indicating that the value is NULL in the database. It is required to disambiguate with .Net null, which basically means 'null pointer' or unallocated variable. Since it is a special value, it requires special handling to convert into other data types, check DB data for null values etc.

.Net uses System.DBNull singleton class to handle database values.

The DBNull class represents a nonexistent value. In a database, for example, a column in a row of a table might not contain any data whatsoever. That is, the column is considered to not exist at all instead of merely not having a value. A DBNull object represents the nonexistent column. Additionally, COM interop uses the DBNull class to distinguish between a VT_NULL variant, which indicates a nonexistent value, and a VT_EMPTY variant, which indicates an unspecified value.

In a relational database, null is used to describe a missing/unknown value, not to be confused with null (Nothing in ) - that's a reference that points nowhere.

322 questions
9
votes
3 answers

How do I set a field to DBNull in Entity Framework

How do you set a field to DBNull in Entity Framework? The fields are strongly typed so you cannot set it equal to DBNull.Value and I did not find any method to set a field to DBNull. It seems like this is a necessary thing to do, but after much…
Eric
9
votes
6 answers

Checking for DBNull throws a StrongTypingException

I am using a dataset to pull data from a DB. One of the fields in a row is NULL. I know this. However, the following vb.net code throws a StrongTypingException (in the autogenerated get_SomeField() method in the dataset designer): If Not…
calico-cat
  • 1,324
  • 6
  • 20
  • 36
9
votes
4 answers

Concise usage of DBNull? (Ternary?)

It seems that there's some type confusion in the ternary operator. I know that this has been addressed in other SO threads, but it's always been with nullables. Also, for my case I'm really just looking for a better way. I'd like to be able to use…
bwerks
  • 8,651
  • 14
  • 68
  • 100
9
votes
3 answers

C# ?? null coalescing operator

I have defined Class Person property Birthday as nullable DateTime? , so why shouldn’t the null coalescing operator work in the following example? cmd.Parameters.Add(new SqlParameter("@Birthday", SqlDbType.SmallDateTime)).Value = …
Lill Lansey
  • 4,775
  • 13
  • 55
  • 77
9
votes
4 answers

How can I include DBNull as a value in my strongly typed dataset?

I've created a strongly typed dataset (MyDataSet) in my .NET app. For the sake of simplicity, we'll say it has one DataTable (MyDataTable), with one column (MyCol). MyCol has its DataType property set to "System.Int32", and its AllowDBNull property…
Beska
  • 12,445
  • 14
  • 77
  • 112
8
votes
8 answers

How do I handle Conversion from type 'DBNull' to type 'String' is not valid

I need some expect advice on how to handle the following:- I have a data field misc_text_2 that is of type varchar(25) and allows NULL. Now if I use the following syntax
mEeRkAt
8
votes
3 answers

How can I assign a DBNull in a better way?

I need to parse a value from a DataRow and assign it to another DataRow. If the input is valid, then I need to parse it to a double, or else add a DBNull value to the output. I'm using the following code: public double? GetVolume(object data) { …
Mike
  • 3,204
  • 8
  • 47
  • 74
8
votes
6 answers

Handling a DateTime DBNull

I have seen many, many versions of this on SO, but none of them seem to quite work for my needs. My data comes from a vendor database that allows null for DateTime fields. First I pull my data into a DataTable. using (SqlCommand cmd = new…
Mike Wills
  • 20,959
  • 28
  • 93
  • 149
8
votes
2 answers

ASP.Net check value with DBNULL

I have the following code foreach (DataRowView dr in Data) { if (dr == System.DBNull.Value) { nedID = 1; } } but i get the following error Operator == cannot be applied to operands…
c11ada
  • 4,302
  • 15
  • 48
  • 62
8
votes
3 answers

DBNull check for ExecuteScalar

The stored procedure for command can return null.Is it correct way to check if the returned value is null or should I also check that obj is null? object obj = command.ExecuteScalar(); int id = -1; if (DBNull.Value == obj) { id =…
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
8
votes
3 answers

How can DBNull not equal DBNull

I have the following line of code if (DBNull.Value.Equals(o) || o != null) where o is object o in row.ItemArray I keep getting an error of --> Xml type "List of xdt:untypedAtomic" does not support a conversion from Clr type "DBNull" to Clr type…
Refracted Paladin
  • 12,096
  • 33
  • 123
  • 233
7
votes
2 answers

Why isn't my DbNull a singleton when I deserialise it using XmlSerialiser?

I've always assumed that DbNull.value was a singleton. And thus you could do things like this: VB.NET: If someObject Is DbNull.Value Then ... End if C#: If (someObject == DbNull.Value) { ... } But recently, I serialised a DbNull instance…
ligos
  • 4,256
  • 2
  • 25
  • 34
7
votes
4 answers

Powershell and SQL parameters. If empty string, pass DBNull

I got this parameter: $objDbCmd.Parameters.Add("@telephone", [System.Data.SqlDbType]::VarChar, 18) | Out-Null; $objDbCmd.Parameters["@telephone"].Value = $objUser.Telephone; Where the string $objUser.Telephone can be empty. If it's empty, how can I…
Tommy Jakobsen
  • 2,323
  • 6
  • 39
  • 66
7
votes
3 answers

is DBNull vs. DBNull.Value.Equals()

I am curious what are the pros and cons of using if(some_value is DBNull) versus if(DBNull.Value.Equals(some_value)). Personally i prefer if(some_value is DBNull) because i find it more readable. I know Microsoft recommends using…
mobal
  • 93
  • 5
6
votes
1 answer

SQL view infers nullable column from non-null table?

I have a Product table with non-null "quantity" (decimal) and "status" (int) columns, and I created a view on this table with the following case expression: SELECT P.ProductTypeId, (CASE WHEN P.StatusId IN (5, 8) THEN 0 ELSE -P.Quantity END)…
naasking
  • 2,514
  • 1
  • 27
  • 32
1 2
3
21 22