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
231
votes
20 answers

Assign null to a SqlParameter

The following code gives an error - "No implicit conversion from DBnull to int." SqlParameter[] parameters = new SqlParameter[1]; SqlParameter planIndexParameter = new SqlParameter("@AgeIndex", SqlDbType.Int); planIndexParameter.Value =…
Relativity
  • 6,690
  • 22
  • 78
  • 128
153
votes
15 answers

Most efficient way to check for DBNull and then assign to a variable?

This question comes up occasionally, but I haven't seen a satisfactory answer. A typical pattern is (row is a DataRow): if (row["value"] != DBNull.Value) { someObject.Member = row["value"]; } My first question is which is more efficient…
ilitirit
  • 16,016
  • 18
  • 72
  • 111
109
votes
6 answers

SQLite equivalent to ISNULL(), NVL(), IFNULL() or COALESCE()

I'd like to avoid having many checks like the following in my code: myObj.someStringField = rdr.IsDBNull(someOrdinal) ? string.Empty : rdr.GetString(someOrdinal); I figured I could just have…
Jason Down
  • 21,731
  • 12
  • 83
  • 117
101
votes
6 answers

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

Is there any difference between null and System.DBNull.Value? If yes, what is it? I noticed this behavior now - while (rdr.Read()) { if (rdr["Id"] != null) //if (rdr["Id"] != System.DBNull.Value) { int x =…
pavanred
  • 12,717
  • 14
  • 53
  • 59
81
votes
12 answers

DBNull if statement

I'm trying to execute a stored procedure and then use an if statement to check for null values and I'm coming up short. I'm a VB guy so please bear with me if I'm making a schoolboy syntax error. objConn = new…
PipBoy
  • 1,127
  • 4
  • 11
  • 16
80
votes
14 answers

handling dbnull data in vb.net

I want to generate some formatted output of data retrieved from an MS-Access database and stored in a DataTable object/variable, myDataTable. However, some of the fields in myDataTable cotain dbNull data. So, the following VB.net code snippet will…
Azim J
  • 8,260
  • 7
  • 38
  • 61
76
votes
6 answers

What is the point of DBNull?

In .NET there is the null reference, which is used everywhere to denote that an object reference is empty, and then there is the DBNull, which is used by database drivers (and few others) to denote... pretty much the same thing. Naturally, this…
Vilx-
  • 104,512
  • 87
  • 279
  • 422
49
votes
6 answers

C# ADO.NET: nulls and DbNull -- is there more efficient syntax?

I've got a DateTime? that I'm trying to insert into a field using a DbParameter. I'm creating the parameter like so: DbParameter datePrm = updateStmt.CreateParameter(); datePrm.ParameterName = "@change_date"; And then I want to put the value of the…
Stewart Johnson
  • 14,281
  • 7
  • 61
  • 70
43
votes
3 answers

Why is this code invalid in C#?

The following code will not compile: string foo = "bar"; Object o = foo == null ? DBNull.Value : foo; I get: Error 1 Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string' To…
mmattax
  • 27,172
  • 41
  • 116
  • 149
41
votes
13 answers

Handle DBNull in C#

Is there a better/cleaner way to do this? int stockvalue = 0; if (!Convert.IsDBNull(reader["StockValue"])) stockvalue = (int)reader["StockValue"];
Andreas
  • 451
  • 1
  • 4
  • 3
30
votes
5 answers

Operator '??' cannot be applied to operands of type 'string' and 'System.DBNull'

I have the following C# code: sqlCommand.Parameters.AddWithValue("@Parameter", table.Value ?? DBNull.Value); But it throws the following compilation error: Operator ?? cannot be applied to operands of type string and System.DBNull Why doesn't the…
hwcverwe
  • 5,287
  • 7
  • 35
  • 63
27
votes
4 answers

What should I use to compare DBNull ? Using DBNull.Value or ToString().IsNullOrEmpty()

I can check for a DBnull on a data row using any of the methods. Either by using if(dr[0][0]==DBNull.Value) //do somethin or by doing if(dr[0][0].ToString().IsNullOrEmpty()) //do something In Both Cases I will be getting same result. But Which…
Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286
27
votes
8 answers

Conversion from type 'DBNull' to type 'String' is not valid

i am receiving this problem Conversion from type 'DBNull' to type 'String' is not valid. Line 501: hfSupEmail.Value = dt.Rows(0)("SupEmail") i am very new to this, i am not really sure what is the exact problem could…
user3571305
  • 281
  • 1
  • 3
  • 4
24
votes
7 answers

Dealing with System.DBNull in PowerShell

EDIT: As of PowerShell 7 Preview 2, -not [System.DBNull]::Value evaluates to $true, thanks to Joel Sallow via pull request 9794 Spending more time pulling SQL data in PowerShell. Running into issues with [System.DBNull]::Value and how PowerShell…
Cookie Monster
  • 1,741
  • 1
  • 19
  • 24
20
votes
9 answers

How to check for NULL in MySqlDataReader by the column's name?

How can I check for a NULL value in an open MySqlDataReader? The following doesn't work; it's always hitting the else: if (rdr.GetString("timeOut") == null) { queryResult.Egresstime = "Logged in"; } else { queryResult.Egresstime =…
rd42
  • 3,584
  • 15
  • 56
  • 68
1
2 3
21 22