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
6
votes
5 answers

Simple way to convert dbNull to a string in VB.NET

I'm looking for a simpler way to check if a value is dbNull and to convert it to an empty string if so. An example of a situation where I need this would be: Dim dt As New DataTable Dim conn As New OleDbConnection(someConnStr) Dim adap As New…
user2276280
  • 603
  • 2
  • 10
  • 24
6
votes
2 answers

DBNull in non-empty cell when reading Excel file through OleDB

I use OleDB to read an Excel file. One of the columns has a "Common" format and contains both strings with letters and values consisting of numbers only. String values are retrieved without problem, but pure numerical values are retrieved as…
flashnik
  • 1,900
  • 4
  • 19
  • 38
6
votes
4 answers

Linq and DBNull - Getting error

I'm getting an error when selecting from a rows.AsEnumerable(). I am using the following code... var rows = ds.Tables[0].AsEnumerable(); trafficData = rows.Select(row => new tdDataDC { …
Hcabnettek
  • 12,678
  • 38
  • 124
  • 190
6
votes
6 answers

.NET DBNull vs Nothing across all variable types?

I am a little confused about null values and variables in .NET. (VB preferred) Is there any way to check the "nullness" of ANY given variable regardless of whether it was an object or a value type? Or does my null check have to always anticipate…
Matias Nino
  • 4,265
  • 13
  • 46
  • 63
5
votes
2 answers

Automatically convert .Net null to DBNull.Value?

I've written a utility to handle adding values to data parameters for hand-rolling sql queries. Consumption looks like utility.Add("SELECT * FROM MyTable WHERE MyColumn = {0}", myVariable, DBType.TheType); or utility.Add("UPDATE MyTable SET…
StarTrekRedneck
  • 1,957
  • 1
  • 15
  • 15
5
votes
2 answers

How to determine if a parameter value was passed to a stored procedure

I want to create a stored procedure (in SQL Server 2008 R2) that will update a record in a table based on the table's PK. The stored proc will have, for example, four parameters: @ID int, @Name nvarchar(50), @Email nvarchar(80), @Phone…
Jed
  • 10,649
  • 19
  • 81
  • 125
5
votes
8 answers

Casting a NULL value

I'm trying to populate a class object with values from a database table. The someObject.Property field is a nullable int type. someObject.Property = Convert.ToInt32(dbReader["SomeField"]); So, if SomeField is null, Convert will give a DBNull error.…
StoneJedi
  • 575
  • 1
  • 8
  • 19
5
votes
4 answers

In PostgreSQL (and maybe other engines), why does the UNION statement consider NULL values the same, while the UNIQUE constraint does not?

I understand that the SQL standard allows multiple NULL values in a column that is part of the UNIQUE constraint. What I don't understand is why the UNION construct (at least in PostgreSQL,) treats NULL values as the same. For example: $ select *…
Shankster
  • 63
  • 6
5
votes
2 answers

Passing blank field value to stored procedure ASP .NET C#

I want to return all rows from a SQL Server 2008 database table into a SQL data source if a textBox field is blank. So I made a stored procedure with an if @date IS NULL clause. Although the stored procedure seems to be working fine in Visual…
user626873
  • 177
  • 4
  • 11
5
votes
2 answers

How do I pass a null value into a parameter for a SqlCommand

Before anyone comments that this has been answered before in other question I KNOW that..but in spite of the answers I have reviewed at Assign null to a SqlParameter Sending null parameters to Sql Server Best method of assigning NULL value to…
Bastyon
  • 1,571
  • 4
  • 21
  • 28
5
votes
3 answers

C#/Oracle10g = null vs DBNull.Value vs String.Empty

I'm making my first forays into Accessing Oracle from C#. I've discovered that that Oracle doesn't like VarChar parameters to have value of null (C#). I would have hoped that there would be some implicit conversion, but I appreciate the…
CJM
  • 11,908
  • 20
  • 77
  • 115
5
votes
2 answers

Filtering DBNull With LINQ

Why does the following query raise the error below for a row with a NULL value for barrel when I explicitly filter out those rows in the Where clause? Dim query = From row As dbDataSet.conformalRow In dbDataSet.Tables("conformal") _ …
Steven
  • 13,501
  • 27
  • 102
  • 146
5
votes
2 answers

How to deal with SqlDataReader null values in VB.net

I have the follwoing code that performs a query and returns a result. However, I looked around and found some examples to take care of null values but I get an error: "Invalid attempt to read when no data is present." I also got the error:…
Joseph.Scott.Garza
  • 133
  • 1
  • 2
  • 8
5
votes
5 answers

Cast a null into something?

I had this interesting discussion today with a colleague. We were debating two pieces of code in C#. Code Snippet 1: if(!reader.IsDBNull(2)) { long? variable1 = reader.GetInt64(2) } Code Snippet 2: long variable1 = reader.IsDBNull(2) ? (long?)…
Bull
  • 701
  • 1
  • 6
  • 13
5
votes
1 answer

What is VB.NET's equivalent of C#'s default keyword?

Possible Duplicate: Bug?? If you assign a value to a nullable integer via a ternary operator, it can't become null While this question may seem like a duplicate of many, it is actually being asked for a specific reason. Take this code, for…
oscilatingcretin
  • 10,457
  • 39
  • 119
  • 206