How to assign ( set ) Null value to DateTime Variable in C# .Net

dotnet
 
on Mar 08, 2013 02:53 AM
24123 Views

How to assign Null value to datetime variable using nullable

I am getting a value from the textbox (I select a date)

but, may be sometimes user not select any value to the datetime textbox

in that cases i compare with null

i do like , but it gives me error

 DateTime? ScheduleDate = txtScheduleDate.Text;

                 

                    if (ScheduleDate.HasValue)
                    {
                        ScheduleDate = Convert.ToDateTime(txtScheduleDate.Text);
                    }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mudassar
 
on Mar 14, 2013 03:03 AM

Do this way

 DateTime? ScheduleDate;
if(!string.IsNullOrEmpty(txtScheduleDate.Text.Trim()))
{             
 ScheduleDate  = Convert.ToDateTime(txtScheduleDate.Text);
}
else
{
   ScheduleDate  = null;
}