OJB1
OJB1

Reputation: 2785

C# & SQL Server : how to Insert DBNull.Value in command parameter if string value is empty?

I've been searching for a couple of hours now and cannot find a solution.

I am inserting some strings into SQL, however on on occasions, the method I use to do this may contain strings that are empty, i.e ""

I therefore want to insert a null value into SQL Server instead.

Firstly I test my method to make sure I am able to insert a DBNull.Value manually by using the following:

cmd.Parameters.AddWithValue("@surname", DBNull.Value);  // THIS WORKS OK

HOWEVER when I try the following below, the method runs without any error exception BUT the SQL Server column does not show a null value, it's just empty with nothing in it:

cmd.Parameters.AddWithValue("@surname", (object)surname ?? DBNull.Value);

I just want an efficient solution without messing about with any more common class extensions and writing lots of extra code. Every answer I see on the forums is just over complicated for what should be such a simple task.

Any ideas? Thanks in advance...

Here is the full method:

public static void AddUserToUserTable(string username, 
        string forename, 
        string surname, 
        string emailAddress, 
        string password, 
        string accountActive, 
        string userGroup)
{
    string cmdText1 = "INSERT INTO Users (Username, Forename, Surname, EmailAddress, Password, AccountActive, UserGroup) VALUES (@username, @forename, @surname, @emailAddress, @password, @userGroup, @accountActive)";

    try
    {
        // The using statement will take care of the disposing of the reader and the command object.
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlCommand cmd = new SqlCommand(cmdText1, connection);

            cmd.Parameters.AddWithValue("@username", username);
            cmd.Parameters.AddWithValue("@forename", forename);
            cmd.Parameters.AddWithValue("@surname", surname, null);
            cmd.Parameters.AddWithValue("@emailAddress", emailAddress);
            cmd.Parameters.AddWithValue("@password", password);
            cmd.Parameters.AddWithValue("@userGroup", accountActive);
            cmd.Parameters.AddWithValue("@accountActive", userGroup);

            connection.Open();
            cmd.ExecuteNonQuery();

            log.Info("NEW User [" + username + "] Created");
        }
    }
    catch (Exception ex)
    {
        log.Error(ex);
    }
}

Upvotes: 3

Views: 7188

Answers (6)

kfwongao
kfwongao

Reputation: 21

Just check from microsoft doc

public System.Data.SqlClient.SqlParameter AddWithValue (string parameterName, object value);

DBNull and string class both Inherited from Object so you can cast them and pass an object type in the second parameter.

One simple suggestion to add DBNull.Value to SQL server if string is empty is the following

Try the following in your code (it works): you also need to know the ?? operator

string sql = "INSERT INTO [table] (columnName) VALUES (?);";
cmd.Parameters.AddWithValue("@columnName", (object)model.value ?? DBNull.Value);

Upvotes: 1

JogiKalpesh
JogiKalpesh

Reputation: 1

try this

string surname = string.Empty;

cmd.Parameters.AddWithValue("@surname", string.IsNullOrEmpty(surname) ? DBNull.Value :(object)surname);

Upvotes: 0

Drew Chapin
Drew Chapin

Reputation: 7989

1) The simplest solution everyone has already mentioned:

cmd.Parameters.AddWithValue("@surname", 
    String.IsNullOrEmpty(surname) ? DBNull.Value : surname);

2) If you can modify the database itself, you could add a trigger to replace empty strings with NULL on INSERT and UPDATE operations. This has an advantage of ensuring consistency if there are other developers and/or applications altering the database..

CREATE TRIGGER User_ReplaceEmptyWithNull
    ON 
        Users 
    AFTER 
        INSERT, 
        UPDATE 
    AS
        UPDATE 
            Users
        SET 
            Users.Forename = IIF(inserted.Forename != '', inserted.Forename, NULL),
            Users.Surname = IIF(inserted.Surname != '', inserted.Surname, NULL)
        FROM 
            inserted INNER JOIN Users
                ON inserted.Username = Users.Username

Disclaimer: I'm not an expert on database triggers. I adapted this from answers on another SO question


3) You could make an extension method for String objects.

namespace YOUR_NAMESPACE
{
    public static class MyExtensions
    {
        public static object OrDBNull( this String value )
        {
            return String.IsNullOrEmpty(value) ? DBNull.Value : value;
        }
    }   
}

...

cmd.Parameters.AddWithValue("@surname", surname.OrDBNull());

4) You could make an extension method for SqlParameterCollection objects.

namespace YOUR_NAMESPACE
{
    public static class MyExtensions
    {
        public static void AddString( this SqlParameterCollection collection, string parameterName, string value )
        {
            collection.AddWithValue(parameterName, String.IsNullOrEmpty(value) ? DBNull.Value : value);
        }
    }   
}

...

cmd.Parameters.AddString("@surname", surname);

Disclaimer: Untested. I probably screwed this up somewhere.

Upvotes: 5

Hakunamatata
Hakunamatata

Reputation: 1275

You can do something like this to cover both empty string and null values.

 String.IsNullOrWhiteSpace(paramvalue) ? DBNull.Value : paramvalue

Upvotes: 0

OJB1
OJB1

Reputation: 2785

quick update, I've just seen another thread and tried the following code below which works for me, not sure if its the bets way but appears fairly similar to what I was trying to achieve:

    cmd.Parameters.AddWithValue("@surname", string.IsNullOrEmpty(surname) ? (object)DBNull.Value : surname);

I was missing the string.IsNullOrEmpty part.

Upvotes: 2

Alex M
Alex M

Reputation: 147

Try this

              string.IsNullOrEmpty(surname)  ? DBNULL.Value : surname

Upvotes: 0

Related Questions