How to pass a null variable to a SQL Stored Procedure from C#.net code

154,108

Solution 1

SqlParameters[1] = new SqlParameter("Date1", SqlDbType.SqlDateTime);
SqlParameters[1].Value = DBNull.Value;
SqlParameters[1].Direction = ParameterDirection.Input;

...then copy for the second.

Solution 2

Use DBNull.Value Better still, make your stored procedure parameters have defaults of NULL. Or use a Nullable<DateTime> parameter if the parameter will sometimes be a valid DateTime object

Solution 3

You can pass the DBNull.Value into the parameter's .Value property:

    SqlParameters[0] = new SqlParameter("LedgerID", SqlDbType.BigInt );
    SqlParameters[0].Value = DBNull.Value;

Just adjust for your two DateTime parameters, obviously - just showing how to use the DBNull.Value property value here.

Marc

Solution 4

Old question, but here's a fairly clean way to create a nullable parameter:

new SqlParameter("@note", (object) request.Body ?? DBNull.Value);

If request.Body has a value, then it's value is used. If it's null, then DbNull.Value is used.

Solution 5

I use a method to convert to DBNull if it is null

    // Converts to DBNull, if Null
    public static object ToDBNull(object value)
    {
        if (null != value)
            return value;
        return DBNull.Value;
    }

So when setting the parameter, just call the function

    sqlComm.Parameters.Add(new SqlParameter("@NoteNo", LibraryHelper.ToDBNull(NoteNo)));

This will ensure any nulls, get changed to DBNull.Value, else it will stay the same.

Share:
154,108
Irfy
Author by

Irfy

Updated on July 05, 2022

Comments

  • Irfy
    Irfy almost 2 years

    Im calling a SQL stored procedure from a piece of C#.net code:

    SqlHelper.ExecuteDataset(sqlConnection, CommandType.StoredProcedure, STORED_PROC_NAME, sqlParameters);
    

    where the sqlParameters variable is defined as:

            SqlParameter[] sqlParameters = new SqlParameter[SQL_NUMBER_PARAMETERS];
    
            Log.Logger.Debug(string.Format("Running proc: {0} ", STORED_PROC_NAME));
    
            SqlParameters[0] = new SqlParameter("fieldID", SqlDbType.BigInt );
            SqlParameters[0].Value = fieldID;
            SqlParameters[0].Direction = ParameterDirection.Input;
    

    I need to now pass in another two parameters to this Stored Proc, (both are of type SqlDateTime), which are going to NULL in this case.

    Thanks,

    IN

  • Beska
    Beska almost 15 years
    You get a +1 for being the first person to mention the default parameter for the stored proc possibility.
  • Gayan
    Gayan about 9 years
    that's good solution i changed static method to extension method for objects public static object ToDbNull(this object value) { if (null != value) return value; return DBNull.Value; }
  • C. Ridley
    C. Ridley almost 9 years
    Except that a DateTime isn't a nullable type, so this won't work for DateTime (as specified in the question), or for any other non-nullable value types, unless you declare all you vars to be Nullable<T> which doesn't seem a particularly good approach.
  • saluce
    saluce over 7 years
    It might be simpler, rathar than having this extension method, to simply use the null coalescing operator: new SqlParameter("@NoteNo", NoteNo as object ?? System.DbNull)