Pass Parameter to NULLABLE DateTime field in C# .Net

Last Reply on Nov 26, 2015 01:46 AM By Shashikant

Posted on Nov 25, 2015 01:47 AM

I have a textbox that I use to gather a date.  I would like to have the database store a null if this field is left blank by the user when submitting. 

I am using the following on my aspx page:


<div class="col-md-3">
                    <strong>Date Submitted to TDI</strong><br />
                    <asp:TextBox ID="sub_tdi_dt" runat="server" Enabled="true"></asp:TextBox>                    
                    <asp:ImageButton ID="sub_tdi_dt_cal_popup" Enabled="false" ImageUrl="Images/calendar.png" ImageAlign="Bottom" runat="server" />
                    <ajaxToolkit:CalendarExtender runat="server" BehaviorID="sub_tdi_dt_CalendarExtender" PopupButtonID="sub_tdi_dt_cal_popup" TargetControlID="sub_tdi_dt" ID="sub_tdi_dt_CalendarExtender"></ajaxToolkit:CalendarExtender>

I have tried using the following in Page_Load, but it doesn't work - I still get '1/1/1900':


DateTime? sub_tdi_dt = null;



Posted on Nov 26, 2015 01:46 AM Modified on on Nov 26, 2015 07:37 AM
jferguson says:
new_rvw_cmd.Parameters.AddWithValue("@sub_tdi_dt", sub_tdi_dt.Text);

 Replace with

new_rvw_cmd.Parameters.AddWithValue("@sub_tdi_dt", !string.IsNullOrEmpty(sub_tdi_dt.Text) ? sub_tdi_dt.Text : DBNull.Value);