Is it possible to pass a parameter that is DB.null and set a value in the database equal to that?

StackOverflow https://stackoverflow.com/questions/8491337

  •  15-03-2021
  •  | 
  •  

Question

I'm pulling data from a database (1 record) and putting each value in a textbox or dropdownlist. Some of these values are DB.null. If they are, I convert each of them to "" (blank). However, once the user has modified the textboxes/dropdown list, they can "Save" the new results. My problem is this:

If the user leaves something blank, I want to be able to maintain that the value is DB.null. Is there a way to do this?

Also, is it possible that if there is a value in a textbox and the user decides to delete it, I can also convert it to DB.Null? Here's what I tried to do to give you guys some idea:

productName != ""
    ? myCommand.Parameters.AddWithValue("@ProductName", productName)
    : myCommand.Parameters.AddWithValue("@ProductName", DBNull.Value);
Was it helpful?

Solution

With SQL Server I believe you can interchangably use null or DBNull.Value to set a value to NULL.

I'd also change your code to this:

myCommand.Parameters.AddWithValue(
    "@ProductName",
    // this requires a cast as ?: must return the same type
    String.IsNullOrWhiteSpace(productName)
        ? (object)DBNull.Value
        : (object)productName
);

Or using null and without an explicit cast:

myCommand.Parameters.AddWithValue(
    "@ProductName",
    ! String.IsNullOrWhiteSpace(productName) ? productName : null
);

OTHER TIPS

It is valid to check for empty string and then use:

myCommand.Parameters.AddWithValue("@ProductName", DBNull.Value)

if it is, like:

if (string.IsNullOrEmpty(ProductName)) {

    myCommand.Parameters.AddWithValue("@ProductName", DBNull.Value);

} else {

    myCommand.Parameters.AddWithValue("@ProductName", ProductName);

}

If you're working with .Net 2.0 or above, consider leveraging the Nullable(Of T) structure. It makes it really easy to test for Null values and you can simply drop the .Value property of the object into your TextBox if they specified a value. These objects default to null, so when you go to update records in the database, if no value was specifed, you leave the variable alone and NULL will make it into your database.

I'll admit there are a little odd to work with at first (you have to remember to use the .Value property when binding to a TextBox) but after that initial hurdle the structure is quite handy if you're going to be dealing with a lot of database fields that can potentially have nullable values.

A bit shorter version:

myCommand.Parameters.AddWithValue(
    "@ProductName", 
    productName == null ? DBNull.Value : (object)productName
);

Best not to tie data-centric types (like DBNull) with your TextBox. While it may make your code look cleaner on the data side there's no built-in way to automatically convert an empty string to and from DBNull.

A clean way on your DB side would be

myCommand.Parameters.AddWithValue(
    "@ProductName",
    productName.IsNullOrEmpty() ? (object)(DBNull.Value) : productName
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top