dbnull

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 tweaking that class to work with Oracle, we've come across an interesting question. Is it bette...

(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. ...

.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 whether it's checking a value type (e.g. System.Integer) or an object? I guess what I'm lo...

C# ADO.NET: nulls and DbNull -- is there more efficient syntax?

I've got a DateTime? that I'm trying to insert into a field using a DbParameter. I'm creating the parameter like so: DbParameter datePrm = updateStmt.CreateParameter(); datePrm.ParameterName = "@change_date"; And then I want to put the value of the DateTime? into the dataPrm.Value while accounting for nulls. I thought initially I'd b...

Most efficient way to check for DBNull and then assign to a variable?

This question comes up occasionally but I haven't seen a satisfactory answer. A typical pattern is (row is a DataRow): if (row["value"] != DBNull.Value) { someObject.Member = row["value"]; } My first question is which is more efficient (I've flipped the condition): row["value"] == DBNull.Value; // Or row["value"] is DBN...

handling dbnull data in vb.net

I want to generate some formatted output of data retrieved from an MS-Access database and stored in a DataTable object/variable, myDataTable. However, some of the fields in myDataTable cotain dbNull data. So, the following VB.net code snippet will give errors if the value of any of the fields lastname, intials, or sID is dbNull. dim ...

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 works. I just wonder, how do I "express" in code that a field is not-null without commenting?...

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 because it doesn't know how to handle a returned null value. I've tried casting the data to th...

How do I handle a DBNull to Boolean conversion in my XSD DataSet?

In my database, I have a few columns in one of my tables that are bit (boolean) values. They are allowed to be NULL since the fields are not always going to contain data. I've gone through the process of creating an XSD DataSet using the table and made sure that the AllowDBNull field is set to True. However, when I pull a down record f...

How do I handle Conversion from type 'DBNull' to type 'String' is not valid

Hi Guys, I need some expect advice on how to handle the following:- I have a data field misc_text_2 that is of type varchar(25) and allows NULL. Now if I use the following syntax <asp:Label ID="lblPrinter" runat="server" Text='<%# iif(eval("misc_text_2") is dbnull.value, "", iif(eval("misc_text_2") like "NA", "None", iif(eval("misc_te...

Problem inserting string or NULL into SQL Server database

I just read the top answer at this post: http://stackoverflow.com/questions/374522/problem-inserting-string-or-null-into-sql-server-database Correct me if I'm wrong, but can the ??-operator not only be used on two variables of the same type, if not I would greatly appreciate it, if anyone could solve my minor problem. I tried to inser...

Dataset field DBNull -> int?

SQLServer int field. Value sometimes null. DataAdapter fills dataset OK and can display data in DatagridView OK. When trying to retrieve the data programmatically from the dataset the Dataset field retrieval code throws a StronglyTypedException error. [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] public int curr_read...

SQLite equivalent to ISNULL(), NVL(), IFNULL() or COALESCE()

I'd like to avoid having many checks like the following in my code: myObj.someStringField = rdr.IsDBNull(someOrdinal) ? string.Empty : rdr.GetString(someOrdinal); I figured I could just have my query take care of the nulls by doing something like this: SELECT myField1, [isnull](myField1, '') FROM myTable1 WHERE myField1 = someCondit...

How do I set a field to DBNull in Entity Framework

How do you set a field to DBNull in Entity Framework? The fields are strongly typed so you cannot set it equal to DBNull.Value and I did not find any method to set a field to DBNull. It seems like this is a necessary thing to do, but after much Google research I found nothing about it. I am trying to set a datetime field in Entity Fra...

LINQ to Dataset DBNULL problem / null reference exception

I have the following LINQ query which always results in an error when my "Remark" column in dtblDetail is null, even though I test if it is NULL. var varActiveAndUsedElementsWithDetails = from e in dtblElements join d in dtblDetails on e.PK equals d.FK into set from...

Powershell and SQL parameters. If empty string, pass DBNull

Hi, I got this parameter: $objDbCmd.Parameters.Add("@telephone", [System.Data.SqlDbType]::VarChar, 18) | Out-Null; $objDbCmd.Parameters["@telephone"].Value = $objUser.Telephone; Where the string $objUser.Telephone can be empty. If it's empty, how can I convert it to [DBNull]::Value? I tried: if ([string]:IsNullOrEmpty($objUser.Tele...

Question about Type Comparison ASP.NET and DBNull

I have a function that pulls articles records from an MSSQL database. Some are URLs to PDFs, and other are actual articles stored in the SQL. The articles that are stored do not have a URL (DBNull) in the record, so I want to be able to parse that. I tried a simple test: If Row.Item("url").GetType Is GetType(DBNull) Then //' do some...

Why isn't my DbNull a singleton when I deserialise it using XmlSerialiser?

I've always assumed that DbNull.value was a singleton. And thus you could do things like this: VB.NET: If someObject Is DbNull.Value Then ... End if C#: If (someObject == DbNull.Value) { ... } But recently, I serialised a DbNull instance using the XmlSerialiser and suddenly it wasn't a singleton any more. Type comparison o...

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? ...

How can I include DBNull as a value in my strongly typed dataset?

I've created a strongly typed dataset (MyDataSet) in my .NET app. For the sake of simplicity, we'll say it has one DataTable (MyDataTable), with one column (MyCol). MyCol has its DataType property set to "System.Int32", and its AllowDBNull property set to "true". I'd like to manually create a new row, and add it to this dataset. I cre...