Recently converted my ASP.NET project from 1.1 to 3.5. Hooray! Currently developing a form which has a handful of optional fields. Normally, I’d go through in my code, adding tons of if statements, checking for empty field, and setting value to 0 if so. So, I’m wondering if it would be best to instead, declare private, nullable variables for each of these fields, then add these as parameters for my database update? Using MSSQLS 2000, already set the corresponding fields to allow nulls.
To clarify: The web form has fields for dollar amounts. The default value on the inputs is 0, but if the user deletes one of those 0’s, leaving the field empty then submits the form, an exception will be thrown at Convert.ToDecimal(MoneyField.Text) in the argument list of the method that submits all this to the database. Is there a cleaner way to do this that I’m missing?
It seems unusual that you would have that many fields that are truly nullable, but if you need to describe “no value” separately to any magic domain value of (for example) an
int, then yes:Nullable<T>may help. Note that you must manually translate fromnulltoDbNull.Valueat the data-layer, asnullon aSqlParametermeans “don’t send this parameter”, not “send the valuenull” (if you see the distinction).