I timed myself today with an ASP.NET app based extensively on code generation (CodeSmith) across the layers: CS templates crank out the business object, the HTML fields, the code that assigns the controls from the business object, packages up the field data, and the code to both update or add the record to SQL. The form has both “update” and “add new” capabilities, 8 fields, 3 sql tables. Less than an hour from start to finish. So that's a good thing.
Then I look at my sql data and can't figure out why the blank form fields are not <NULL> as they're supposed to be. I track it down to the ADO datatable entry code, an example being:
drw2["phone"] = oSQLUtils.blnIsNotNull(useredit.phone) ? oSQLUtils.CleanPhone(useredit.phone) : System.DBNull.Value.ToString();
I stepped through both the blnIsNotNull and CleanPhone methods. Everything correct. But where's my NULL???? I spent less than an hour building a completely functional form, and am going on 45 minutes figuring out why my empty form fields are not being stored as null.
Seems I need some up-close-and-personal time with the .NET null concepts. Between System.DBNull.Value.ToString(), string.Empty, and plain old null, as far as how they differ in various contexts, I get confused.
The System.DBNull.Value triggered a runtime object conversion to string error, so that wasn't it. Finally after exploring all other possibilities in the statement, I used “null.” As in
drw2["phone"] = oSQLUtils.blnIsNotNull(useredit.phone) ? oSQLUtils.CleanPhone(useredit.phone) : null;
That did it. I should probably study more on .NET “null“ today, but yesterday while nordic tracking I read through Petzold's Programming Windows in C# chapters on Points, Rectangles, Pens, OnPaintEventArgs, and other exciting topics. I can't do “null“ today.