Luckily I'm bald, otherwise I'd be pulling all my hair out! Please help.

  • Thread starter Thread starter Big Dave
  • Start date Start date
B

Big Dave

There has got to be a good strategy for dealing with null values in C#,
but I can't find it anywhere.

I'm mainly concerned with allowing nulls in DateTime and Int data types.
The standard types aren't nullable, the sqltypes aren't serializable,
I've tried the nullable types on this web site
(http://nullabletypes.sourceforge.net/), I can't bind them to an asp.net
datagrid or format the dates, I've tried to find info on how to create
my own data types and that was unsuccessful, and I've tried to use a
default fake null value "1/1/0001" for dates, but I can't blank out that
value when I databind.

I don't know what else to do, does anybody have a good strategy for
dealing with this? Thank you very much for any assistance!!!!!

Big Dave
 
Assuming asp.net...
One incredibly ineffecient way to overcome this is to use the databound
event and compare the cell in the row for DateTime.Min and if equal set the
cell to string.Empty.
 
I think (not positive without further testing) I've found a possible
solution, I thought I'd post it in case anybody else needs it, or if
anybody has a better idea.

I added this function to an abstract class that inherits from
System.Web.UI.Page, then I inherit from that abstract class on all my
pages.

public string NullDateTime(string value)
{
if(value==DateTime.MinValue.ToString())
{
return String.Empty;
}
else
{
DateTime curDt = DateTime.Parse(value);
return curDt.ToShortDateString();

}
}

Then, in the datagrid, I convert the columns to template columns and set
up the databinding statement as follows:

NullDateTime(DataBinder.Eval(Container, "DataItem.MyDate", ""))

This seems to work, but I'm definitley open to a better solution if
anyone knows of one.

Thanks again for your help.
Big Dave
 
Hi,

Well it's very simple indeed.

As you said there is not null, so the first step is selecting a "null"
value , like DateTime.MinValue

Then you can check for this value in the binding process, depending of how
you bind your grid, You can use DataItemBound event or using a protected
method , I will show you the second one:

Now, I'm binding to a strong typed collection of business classes, but it's
the same concept

<asp:datagrid id=recordgrid runat="server" ShowHeader="false">

<columns>
<asp:templatecolumn ItemStyle-VerticalAlign="Top" ItemStyle-Width="100"
ItemStyle-HorizontalAlign="Center" >
<itemtemplate>
<span ><%# DaysLefts( ((CtpRecord)Container.DataItem).StartDate )
%></span>
</itemtemplate>
</asp:templatecolumn>

Then in the code behind:
protected string DaysLefts( DateTime date)
{

if ( date == DateTime.NimValue )
{
return "N/A";
}
else
return DateTime.Now; /* do the calculation here*/
}


Cheers,
 
This problem has been bugging me too.

In Expert C# Business Objects, the author recommends creating a new
datatype which handles nulls; it has a Text property that returns
String.Empty if the internal DateTime = DateTime.MinValue. He then
binds to the Text property of that class.

The book is from APress, a very good read in general.

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top