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

Which of IsDBNull and IsNull should be used?

If in VB.NET I have DataRow and I want to test whether a column value is Null, should I use: myDataRow.IsNull("Column1") OR IsDBNull(myDataRow("Column1"))
CJ7
  • 22,579
  • 65
  • 193
  • 321
17
votes
4 answers

Finding null value in Dataset - DataRow.IsNull method vs ==DbNull.Value - c#

What are the benefits of using the c# method DataRow.IsNull to determine a null value over checking if the row equals DbNull.value? if(ds.Tables[0].Rows[0].IsNull("ROWNAME")) {do stuff} vs if(ds.Tables[0].Rows[0]["ROWNAME"] == DbNull.value) {do…
Jarrod
  • 1,535
  • 3
  • 16
  • 19
17
votes
3 answers

Implicit conversion from data type nvarchar to varbinary(max) is not allowed

I get this exception when I try to insert a DBNull.Value into a nullable varbinary(max) field: Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this query. This is my…
Pascal
  • 12,265
  • 25
  • 103
  • 195
15
votes
6 answers

Null value in a parameter varbinary datatype

How can I add a null value in a parameter varbinary datatype? When I execute the following code: using (SqlConnection myDatabaseConnection1 = new SqlConnection(myConnectionString.ConnectionString)) { using (SqlCommand mySqlCommand = new…
Karlx Swanovski
  • 2,869
  • 9
  • 34
  • 67
14
votes
3 answers

How to manage parsing an null object for DateTime to be used with ADO.NET as DBNULL

I have two DateTime objects, BirthDate and HireDate. They are correctly formatted as a string and when I pass them through to my data access layer, they need to be parsed into a DateTime object. DateTime hD = DateTime.Parse(hire); …
GivenPie
  • 1,451
  • 9
  • 36
  • 57
13
votes
4 answers

C# Database Access: DBNull vs null

We have our own ORM we use here, and provide strongly typed wrappers for all of our db tables. We also allow weakly typed ad-hoc SQL to be executed, but these queries still go through the same class for getting values out of a data reader. In…
David Wengier
  • 10,061
  • 5
  • 39
  • 43
13
votes
4 answers

Cannot implicitly convert type 'decimal?' to 'decimal'.

sdr is my sqldatareader and I want to check that the curPrice value which is of type decimal is null. inrec.curPrice = sdr.IsDBNull(7) ? (decimal?)null : sdr.GetDecimal(7); This is the error message I am getting: Cannot implicitly convert type…
user1270384
  • 711
  • 7
  • 24
  • 53
12
votes
5 answers

Null safe way to get values from an IDataReader

(LocalVariable)ABC.string(Name) = (IDataReader)dataReader.GetString(0); This name value is coming from database. What happening here is if this name is null while reading it's throwing an exception? I am manually doing some if condition here. I…
kumar
  • 2,944
  • 18
  • 60
  • 89
12
votes
3 answers

SqlDataReader Best way to check for null values -sqlDataReader.IsDBNull vs DBNull.Value

I want to retrieve decimal values from the database and I would like to know which is the recommended way to check for null values. I have seen on MSDN - DBNull.Value Field that this check is rarely used. Thus, is the reader.IsDBNull the best/most…
diver
  • 352
  • 1
  • 4
  • 12
12
votes
3 answers

Passing DBNull.Value and Empty textbox value to database

I have some textboxes on my page which can be empty because they are optional and I have this DAL code parameters.Add(new SqlParameter("@FirstName", FirstName)); parameters.Add(new SqlParameter("@LastName", LastName)); parameters.Add(new…
codingbiz
  • 26,179
  • 8
  • 59
  • 96
11
votes
3 answers

ExecuteScalar returns null or DBNull (development or production server)

I'm trying to add a column to an existing DataRow in C#. Afterwards the column will be filled with a single value from my database. DataRow dr already exists and column "COLNAME" also exists. comTBP is my SqlCommand. dr["COLNAME"] =…
Yoni
  • 325
  • 2
  • 7
  • 15
11
votes
6 answers

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

Does any one have a preference on how to check if a value is DBNull? I've found these two statements give me the results I want, but just wondering if there's a preference? if (any is System.DBNull) same as: if (any == System.DBNull.Value) Thanks!
Ricardo Villamil
  • 5,031
  • 2
  • 30
  • 26
10
votes
4 answers

Dealing with DBNull.Value

I frequently have to deal with DataTables connected to grid controls, custom updating always seems to produce a lot of code related to DBNull.Value. I saw a similar question here but think there must be a better answer: What is the best way to deal…
PeteT
  • 18,754
  • 26
  • 95
  • 132
10
votes
2 answers

Operand type clash: nvarchar is incompatible with image

I'm using a SQL Server 2008 stored procedure to create a new record with this syntax: cmd.Parameters.Add("@photo", DBNull.Value) cmd.ExecuteNonQuery() but the result is a: Operand type clash: nvarchar is incompatible with image Photo is not…
ferer
  • 101
  • 1
  • 1
  • 3
9
votes
9 answers

How to safely cast nullable result from sqlreader to int?

I have a table which contains null values and I need to get data from the table using SqlDataReader. I can't figure out how I can safely cast DBNull to int. I'm doing it in this way at the moment: ... reader = command.ExecuteReader(); while…
Burjua
  • 12,506
  • 27
  • 80
  • 111
1
2
3
21 22