Click here to Skip to main content
15,922,007 members
Articles / Programming Languages / C#

Handling DBNull.Value

Rate me:
Please Sign up or sign in to vote.
4.11/5 (2 votes)
20 Oct 2014CPOL 12.3K   2   2
Using DBNull.Value in C# null-coalescing and ?: operators.

Every ASP.NET developer uses DbNull.Value when writing code to communicate with database. But it could sometimes be quite painful when you want to write with a null-coalescing or ?: operator. The same would be when getting something from database and type checking.

For example, in this code snippet, I wrote a function which stores a comment in database. As userWebsite is an optional parameter, so it could be null and if database expects null, then you need to send DBNull.Value. To make it adjustable in ?: operator, you just have to cast into an object. like:

C#
Value = userWebsite != null ? userWebsite : (object)DBNull.Value

I tested this code in fw 4.0 only, so I’m not sure about older versions.

C#
internal int SaveComment(string name, string email, string comment, string? userWebsite)
        {
            SqlCommand _command = new SqlCommand
            {
                Connection = new SqlConnection { ConnectionString = "connection string" },
                CommandType = CommandType.StoredProcedure,
                CommandText = "csp_comment_save",
            };
            _command.Parameters.AddRange(new SqlParameter[] { new SqlParameter 
            { ParameterName = "name", Value = name, SqlDbType = SqlDbType.VarChar, Size = 100 },
            new SqlParameter { ParameterName = "email", Value = email, 
            SqlDbType = SqlDbType.VarChar, Size = 50 },
            new SqlParameter { ParameterName = "comment", Value = comment, 
            SqlDbType = SqlDbType.VarChar, Size = 255 },
            new SqlParameter { ParameterName = "website", Value = userWebsite != null ? 
            userWebsite : (object)DBNull.Value, SqlDbType = SqlDbType.VarChar, Size = 20 }});
            _command.Connection.Open();
            int result=_command.ExecuteNonQuery();
            _command.Connection.Close();
            return result;
        }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
My name is Muhammad Hussain. I'm a Software Engineer.
blog

Comments and Discussions

 
QuestionI sugestion to user "?? - null-coalescing operator" Pin
Kim Togo30-Oct-14 0:05
professionalKim Togo30-Oct-14 0:05 
Msdn site ?? Operator (C# Reference)[^]

The sentence:
C#
Value = userWebsite != null ? userWebsite : (object)DBNull.Value


Can be rewritten to:
C#
Value = userWebsite ?? (object)DBNull.Value

Makes it easier to read.

modified 30-Oct-14 9:17am.

AnswerRe: I sugestion to user "?? - null-coalescing operator" Pin
M.Hussain.30-Oct-14 3:19
M.Hussain.30-Oct-14 3:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.