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

C# : return dbnull.value or system.type from function?

I want to create a function (c#): int PutInt (int? x) { if (x.HasValue) return x.Value; else return DBNull.Value; } but there is no cast from int to DBNull.Value. Is there any way to create such a function ?
dilix
  • 3,761
  • 3
  • 31
  • 55
3
votes
4 answers

How does one reliably check if a nullable property contains no data? DBNull is killing me

I've begun using Massive, by Rob Conery. Awesome little "ORM" and very performant. However, I'm running into issues with System.DBNull comparisons on my nullable fields. For example, I want to check if a property matches another property (in this…
Chaddeus
  • 13,134
  • 29
  • 104
  • 162
3
votes
2 answers

Why does IS NULL work but SQL Parameter with DBNull.Value for DateTime does not?

I have a C# console application that is performing a database lookup on SQL Server 2014. It queries against a table with a DateTime column called EffectiveDate which allows nulls. If I write a standard SQL query that uses 'WHERE EffectiveDate IS…
sldorman
  • 145
  • 2
  • 11
3
votes
1 answer

Can't use DBNull in .NET Standard 1.4?

I am creating a class library that will be used in a WPF project and a .NET Core project. For the following code: public class MyClass { private void MyFunction(object o) { if (o == DBNull) { …
yitzih
  • 3,018
  • 3
  • 27
  • 44
3
votes
7 answers

Why can't I use '??' operand for System.DBNull value?

I have an Oracle data table fetching columns that be null. So I figure to keep the code nice and simple that I'd use the ?? operand. AlternatePhoneNumber is a string in my C# model. AlternatePhoneNumber = customer.AlternatePhoneNumber ??…
Mike
  • 4,257
  • 3
  • 33
  • 47
3
votes
4 answers

How to programmatically set NullValue property of DataColumn of ADO.Net DataTable

I have many new wizard-created DataTables in my project, which I need to extract data from. There are many string columns with dbnull values in these tables, which I want to extract as empty strings. So I went ahead and changed the NullValue…
Peter Brennan
  • 1,366
  • 12
  • 28
3
votes
2 answers

invalid cast exception when trying to transform SQL int -> c# int?

Hi I'm currently using the following to get the columns of a sql server database public IDataReader myReader() { DbCommand = new SqlCommand("Select * from Mydatabase"); SqlConnection con = new SqlConnection(connectionstr); con.Open(); …
Thomas
  • 2,886
  • 3
  • 34
  • 78
3
votes
2 answers

Is DBNull.Value required for nullable types as SqlCommandParameter.Value

Which approach is recommended: void Add(int? value) { command.Parameters.Add("@foo").Value = value; } or void Add(int? value) { command.Parameters.Add("@foo").Value = (object)value ?? DBNull.Value; }
abatishchev
  • 98,240
  • 88
  • 296
  • 433
3
votes
4 answers

How to assign a value to sqlparameter NULL values without checking for null or 0?

The following check is needed to check for null values. is there a way i can do it directly? if(node.ID==0) { cmd.Parameters["@ID"].Value = DBNull.Value; } else { cmd.Parameters["@ID"].Value = node.ID; } is there way we can…
Dexters
  • 2,419
  • 6
  • 37
  • 57
3
votes
3 answers

cannot insert DBNULL value into sql server

Why i get this error? Object cannot be cast from DBNull to other types. when i inserted null value for a numeric datatype in sql server 2005 Here is my code, if (MeasurementTr.Visible == true) { materialIn.measurementId =…
bala3569
  • 10,832
  • 28
  • 102
  • 146
3
votes
3 answers

Is there a way to check if column supports null values from datareader?

Columns of a table in db can store a null values (as DBNulls). Is there a way I can get this info from IDataReader or DbDataReader? using (var reader = command.ExecuteReader()) { //can I get the column info like if it supports null value if I…
nawfal
  • 70,104
  • 56
  • 326
  • 368
3
votes
1 answer

System.DBNull with data contract name not expected in WCF

Possible Duplicate: Why isn’t my DbNull a singleton when I deserialise it using XmlSerialiser? I have an object array that I am passing to a WCF call that has DBNull.Value as one of the values. WCF is apparently choking on it because it doesn't…
Lurker Indeed
  • 1,521
  • 1
  • 12
  • 21
3
votes
2 answers

Why is this program not entering my ElseIf statement -- System.DBNull

I imported an Excel file into an ASP.NET DataTable. Some of the values are blank, specifically at indexes 2 and 3, and I would like to set those blank fields to 0. When debugging, the values for row.item(2) and row.item(3) are both System.DBNull. …
user1077685
3
votes
3 answers

How do I handle a DBNull DateTime field coming from the SQL Server?

I am getting this error when I retrieve a row with a null DataTime field: 'srRow.Closed_Date' threw an exception of type 'System.Data.StrongTypingException' How do I properly handle these?
Mike Wills
  • 20,959
  • 28
  • 93
  • 149
2
votes
3 answers

Oracle 11g odp.net driver issues with Null value

I am having issues comparing null values with oracle 11g odp.net driver. It works fine with oracle 10g odp.net driver. If a column is null in the database, then in datarow it has a string value of null. This code fails: int parentId = …
Naveen Chakravarthy
  • 819
  • 2
  • 15
  • 30