Click here to Skip to main content
15,917,997 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
IFormatProvider provider1 = new System.Globalization.CultureInfo("en-CA", true);
       String reservedate = txtreservedate.Text.Trim().ToString();
       if (!string.IsNullOrEmpty(reservedate))
       {
           DateTime datereservered = DateTime.Parse(reservedate, provider1, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
           objvehiclePL.reserveddate = datereservered;
       }
       else
       {
           objvehiclePL.reserveddate = Convert.ToDateTime(null);
       }


here i get exception at else condition .. please help me?

exception

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
Posted
Updated 6-Apr-14 20:05pm
v3

Hi,
You cannot convert a null value into datetime format.

This would help you.
How to convert null string to datetime[^]
 
Share this answer
 
Use TryParse method, see the following example:
C#
using System;

public class Program
{
    public static void Main()
    {
        string dateString = null;
        //string dateString = "";
        //string dateString = "2014-4-7";

        DateTime dt;

        Console.WriteLine(DateTime.TryParse(dateString, out dt));

    }
}

You should always use TryParse if you allow users to enter date for invalid date, including null and empty string.
My suggestion is use a datepicker or calender control which will always returns a valid date and save you all this hassle.
 
Share this answer
 
Just pass null value as shown.

objvehiclePL.reserveddate = System.DBNull;
 
Share this answer
 
Comments
[no name] 7-Apr-14 3:25am    
hi arjun..
here reserveddate has datetime datatype..
if i enter "system.DBNull" shows an error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900