Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hi,
I want to convert a null string into datetime.I am getting the string st from database. When i find st= something then it works ok. But when it returns null or empty then i m getting errors.
string st=nul;
Lavel1.Text= Convert.ToDateTime(st).ToShortDateString();

Codes are written in C#;

Thanks in advance.
Posted
Updated 7-May-19 6:21am
v2

Can't you just use an if to check the null value?
If it is null, make your textbox blank, else put the value.
 
Share this answer
 
This might help, use the tryparse function of DateTime class

C#
string st = null;
DateTime dateTime;

if (string.IsNullOrEmpty(st))
    Lavel1.Text = string.Empty;
else if( DateTime.TryParse( st , out dateTime))
{
    Lavel1.Text = dateTime.ToShortDateString();
}
 
Share this answer
 
Check the discussion Want to pass null value for Datetime[^]
 
Share this answer
 
try

C#
string str = null;
            if (str == null)
            {
                TextBox1.Text = DateTime.MinValue.ToLongDateString();
            }
 
Share this answer
 
Comments
Prerak Patel 24-Mar-11 2:47am    
Getting null from database and display it as minimum date value is not a good idea.
Mahendra.p25 24-Mar-11 2:50am    
He just asked How to convert a null value into short datetime and i have answered for that.

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



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