DBNull values on an ASP.NET MVC view
I am writing a simple ASP.NET MVC web app. At this point, I am just trying to display a strongly typed DataSet on the automatically generated view. However, some of the values in the DataSe开发者_开发技巧t are null and cause exceptions.
I am wondering how this simple scenario is handled by others. It seems excessive to sanitize all the values in the dataset and besides I am going to end up with a lot of datasets in the final product. I am using DataSets because I have an Oracle backend, so Entity models are kind of out - I'd rather not use sample providers or dish out money for a commercial solution.
If there is no easy solution for DataSets, that's fine. I just wanted to check before I plunge into some serious customization. I'd like to keep things as automated and conventional as possible.
Thanks in advance.
Would it be out of the question to do something like:
<%= (eventClass["MyColumn"] != DBNull.Value) ? eventClass["MyColumn"] : "" %>
You could also convert that into a simple extension method to save space and keystrokes.
HTHs,
Charles
If you can modify your db, you could use the coalesce function to guarantee some non-null default value:
select coalesce(first_name, '') as first_name, ...
or
select coalesce(parent_id, 0) as parent_id, ...
精彩评论