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

Confused about null strings and DBNull

I'm looking at some code for a .net program I'm working on. The previous author of the program gave the code to me over a year ago. Suddenly, I am seeing an exception thrown from code that I haven't touched: if ((string)row["LevelName"] !=…
Vivian River
  • 31,198
  • 62
  • 198
  • 313
4
votes
3 answers

LINQ Replace DBNull with Blank String

In this example, an error is raised if either row.FirstName or row.LastName are NULL. How do I rewrite the Select clause, to convert a DBNull value to a blank string ""? Dim query = From row As myDataSet.myDataRow in myDataSet.Tables("MyData") _ …
Steven
  • 13,501
  • 27
  • 102
  • 146
4
votes
1 answer

How to use DbNull.Value in conditional assignment to column value?

I'm constructing a Command object, with parameters, to set column values. I'd like to use a simple conditional to set a certain nullable int column to either an integer value or DbNull.Value, depending on whether a variable is null or…
ingredient_15939
  • 3,022
  • 7
  • 35
  • 55
4
votes
2 answers

Setting an empty string to null on Insert into database

I haven't been able to find the right solution for this issue, and I know it's so simple but I have forgotten how to do it. I have a form with one textfield field that is not required by the user to fill in. I want to insert NULL into the database,…
Dejsa Cocan
  • 1,539
  • 3
  • 19
  • 56
4
votes
2 answers

How to use collasce null operator with DbNull.Value?

Well this is one of method to handle DBNull.value, But I want a syntax using null-coalescing operator to handle DBNull.value This will work decimal UnitPrice = row["UnitPrice"] == DBNull.Value ? 0.00m : (decimal)row["UnitPrice"]; Well I have tried…
4
votes
5 answers

Getting C# To Insert Defaults Into SQL Server

How can I INSERT values into SQL Server that are stored in a string[] such that some of the values should be disregarded in favor of the values stored in SQL Server as default constraints on the table? What do I need to pass(e.g. NULL or something…
Ian Best
  • 510
  • 1
  • 11
  • 23
4
votes
4 answers

avoid checking for DataRow.IsDBNull on each column?

My code is 2x longer than it would be if I could automatically set IsDBNull to "" or simply roll over it without an error. This is my code: Dim conn As New SqlConnection conn.ConnectionString = Module1.DBConn2 Dim sqlCommand = New SqlCommand("SELECT…
user1382306
4
votes
3 answers

Vb.net Convert Integer DBNULL to 0 - error

I'm having this method: Private Function convertInteger(intInteger As Object) As Integer If IsDBNull(intInteger) Then convertInteger = 0 Else convertInteger = cInt(intInteger) End If End Function But it returns this…
MMM
  • 311
  • 5
  • 14
  • 30
3
votes
2 answers

using datareader with dbnull values in .net4

I heard there is a field extension method in framework 4 that permits one to receive null values from a datareader, without having to go through the process of first testing if not null then ... etc. There's information about the extension method…
Yugmorf
  • 320
  • 1
  • 6
  • 20
3
votes
3 answers

Is it bad to have a column empty as NULL often in a table?

Is it bad to have a column empty as NULL often in a table? comment table comment_id member_id user_id 1 1 NULL 2 1 NULL 3 1 NULL 4 1 NULL 5 …
Run
  • 54,938
  • 169
  • 450
  • 748
3
votes
1 answer

Converting a DBNull to boolean when binding to checkbox in a detailsview control

This is kind of silly but I have a DetailsView that binds to a record in my database using a sqlDataSource. My problem is that the field I'm binding to is a bit field, (i,e 1 or 0) that at present allows nulls. I realize this needs to change but I…
gsirianni
  • 1,334
  • 2
  • 18
  • 35
3
votes
2 answers

Check Constraints in SQL- Specify a value could be null or a constraint

I am trying to incorporate a check constraint in SQLite where the requirement is the following: The value could be null If the value is not null then it should be greater than 3. So, in my create table I wrote create table T(A real check(A = null…
Vinayak Agarwal
  • 1,350
  • 3
  • 15
  • 28
3
votes
2 answers

System.IndexOutOfRangeException on SQLDataReader Value Using C#

I have a SQLDataReader that returns three integers. However, there are occasions when two of the integers will return null values. To get round this I wrote the following: int shoppingCartHeadID = 0; int billID = 0; int delID =…
ComfortablyNumb
  • 1,448
  • 10
  • 37
  • 64
3
votes
2 answers

Linq to DataTable - Cannot cast DBNull

New to Linq, so apologies if this is basic. This query is throwing up the error {"Cannot cast DBNull.Value to type 'System.Int64'. Please use a nullable type."} when I enumerate the results. private void AddLevels(long rootid) { var…
David M
  • 2,763
  • 4
  • 21
  • 24
3
votes
3 answers

?? operator in system.DBNull

Is there an operator or built in function to simplyfy this: myVal = object1.object2.something(a,b).dataColumn.toString()==""?object1.object2.something(a,b).dataColumn.toString():"-"; I know i can do something like: string str =…
tutetimas
  • 31
  • 3