How do you handle nulls and strings from Sql to a string value?
On a web page if the type is a VarChar going to a textbox, you can:
textbox.text = user["stringValue"];
But if an int value that could be null you need to test for DBNull.Value
first and just ignore it if it is a Null. This works fine for updating as
you can tell if the int is supposed to be null or not (if textbox empty -
null).
if I am reading into an int it is a pain as you have to make up a value for
null (usually 0, but not always) -as we talked about in another post:
int param7;
if (parameters[7] == DBNull.Value)
{
// ... handle null case ...
param7 = -1;
}
else
{
param7 = (int)(parameters[7]);
}
How do I handle a string?
I had thought, I wouldn't have to worry about it (like the textbox.text),
but that is not the case I still have to:
string param7;
if (parameters[7] == DBNull.Value)
{
// ... handle null case ...
}
else
{
param7 = (string)(parameters[7]);
}
What is the best way to handle a string? Make it "" if null?
Of course the problem is the same as int, if you send the data back to Sql,
you will now have a value in that field, that you may not want. You may
want it null. Dates are a good example here. I may not want a date in a
DateTerminated field if the person is still working.
Thanks,
Tom