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

Parsing a DBNULL value into double

I use the following line to convert the datarow value into double. double.parse(Convert.ToString(datarow)); If the datarow is DBNULL, I am getting the following exception: 'double.Parse(Convert.ToString(data))' threw an exception of type…
Venkat
  • 2,549
  • 2
  • 28
  • 61
2
votes
0 answers

Returning a DBNull for a bound MaskedTextBox whenever type validation fails?

I have a MaskedTextBox control with the mask "00/00/0000", the PromptChar "_" and the ValidatingType of DateTime, bound to a DateTime column in a database that can also accept null values. I'd like the user to have to enter either a valid date, or…
Rezzie
  • 4,763
  • 6
  • 26
  • 33
2
votes
6 answers

What is the best practice for handling null int/char from a SQL Database?

I have a database that hold's a user's optional profile. In the profile I have strings, char (for M or F) and ints. I ran into an issue where I try to put the sex of the user into the property of my Profile object, and the application crashes…
Matt R
  • 2,577
  • 5
  • 30
  • 46
2
votes
2 answers

Not-Null constraints in POCO Objects

I am currently writing an financial application, and we have a pretty standard customer table. It consists of many mandatory fields, and some optional like Cell/Fax etc.. I'm using NHibernate as a ORM and have all the mappings right. It already…
Tigraine
  • 23,358
  • 11
  • 65
  • 110
2
votes
2 answers

Simplest way to check for DBNull and null at once

I'm querying a SQL Server database through C# to return a number from a specific row of a table. The function's supposed to return the number, otherwise it returns 0. The issue is that sometimes the DB value is null, and sometimes it returns nothing…
MikeOShay
  • 522
  • 1
  • 7
  • 17
2
votes
1 answer

SetField cannot cast DBNull.Value to System.Int32

I'm still very new to C# so please bear with me. I have an Access database with a table that looks like this: ID1 ID2 Name ---------------------- 1111 1234567 Joe 2222 1234567 Patricia 3333 7654321 Laurie None of the fields contain null…
Alex A.
  • 5,466
  • 4
  • 26
  • 56
2
votes
3 answers

VB DBNull solution with for and without if statement

There are many blanks in table and I would like for those blanks (DBNull) in label values to be ignored and presented as blank values. Also with adding and changing table through web form it would be hard to control every single input (23 columns of…
Jovica
  • 450
  • 9
  • 32
2
votes
6 answers

Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

I think I am suppose to use something like this but I am not sure how to modify it to work for DateTime because upon doing so it tells me DateTime can not be null. So how can I change this or is there a better method to resolve this issue? Thank you…
Kpt.Khaos
  • 673
  • 3
  • 14
  • 37
2
votes
4 answers

Single line if null or int return

I'm trying single line if statement as follow and I'm getting error justifiably.How should i do it? int? n; n = (reader[0] == null)? null : Convert.ToInt32(reader[0]);
Goran Zooferic
  • 371
  • 8
  • 23
2
votes
3 answers

How to return a null value from a database

I am trying to populate a combobox with a list my query returns. When I execute my program it gives me a specified cast is not valid error ( I have it execute on page load event). Every field in the database I have to work with can be null except…
Kpt.Khaos
  • 673
  • 3
  • 14
  • 37
2
votes
1 answer

Unable to send null parameter to Sybase Stored Procedure using .Net ASE native ADO client

I'm having and error when I try to call a Sybase stored procedure provinding a null parameter. I wrote a sandbox (see code bellow) When I run the code, I get "Unsupported parameter type" exception. The only way the code works is by removing the null…
2
votes
3 answers

If, else statement to override a null in webmatrix

I thought this would be the proper code to replace a null but it gives me an error "The name 'subt2' does not exist in the current context" var SQLSELECT2 = "SELECT SUM(Price) FROM ProductEntered WHERE TransID=@0"; var sub2 =…
James
  • 83
  • 10
2
votes
2 answers

RowFilter of DataView for DBNull values of object type

How would the filter string looks like for DBNull values in DataTable. "IsNull("Column1", 'Null Column')='Null Column'" "IsNull("Column1", 'Null Column')<>'Null Column'" This string to filter Null values in the Column1. It works fine if it is a…
Sankarann
  • 2,625
  • 4
  • 22
  • 59
2
votes
3 answers

Best way to substitute empty string for DBNull?

If a column in a DataRow might be DBNull, is the following the shortest way to substitute an empty string for DBNull? Dim result As String = if(isDBNull(dataRow1("column1")), "", dataRow1("column1"))
CJ7
  • 22,579
  • 65
  • 193
  • 321
2
votes
2 answers

DBNULL.Value returns always True While Getting Values from Database

I am trying to avoid database null values with 0 and if it is not null then get the original values. but I am having some issues . Here is my Sample Code: int Value = 0; for(int i = 0; i < tblValue.Rows.Count; i++) { if…
John Sheedy
  • 287
  • 1
  • 8
  • 26