Saving null values to a DateTime field

It seems that  because datetime is a value type it is impossible to save a null value.  If you use 2-way databinding in formview for saving dates. You will have problems even when just using a calendar control. This is the solution I found to my problem

In the html view change the Bind(”Date_Column”) into Eval(”Date_Column”)

<

asp:TextBox ID=”Timesheet_DateTextBox” runat=”server” Text=’ Width=”124px”>FONT color=#800000>asp:TextBox>

should then become

<

asp:TextBox ID=”Timesheet_DateTextBox” runat=”server” Text=’ Width=”124px”>FONT color=#800000>asp:TextBox>

I also want to store null values for unentered dates. A string you can null but this will give an exception like : System.String not a valid DateTime

i.e. for the objectdatasource inserting event i do it :

protected

void odsTimesheet_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{
     e.InputParameters[0] =
Guid.NewGuid();
     e.InputParameters[4] = SetDate(“Timesheet_DateTextBox”);
}

private object SetDate(string controlname)
{
     TextBox tb = (TextBox)fvProject.FindControl(controlname);
     return (tb.Text.Trim().Length == 0) ? (object)null : (object)DateTime.Parse(tb.Text);
}

If someone knows a better solution or a codeless one please let me know.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>