Click here to Skip to main content
15,884,062 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two labels a windows form.

label1 is for displaying EnteredDate and label2 for UpdatedDate for a member,

i inserted date in db as datetime.toshortstring() to get only date.
these label get values from db using datareader.
while displaying also i used datetime.toshortstring();

Now problem i got is Updateddate is null unless the member is updated.

so when any row is clicked in datagridview of member whose updated date is null, system crashed saying invalid string for datetime format.

without toshortstring function, time is also displayed which is not required.

i used datatype in sql database as Date.

i need only date to be displayed in label which is stored in database.

any hints would be appreciated.

thanks in advance
Posted
Comments
Dr.Walt Fair, PE 12-Jul-11 22:40pm    
Is the error when reading from the DB and displaying or when trying to save back to the DB?

When you read it back in, examine the date for a null value. If it is null set a tmpStrDate (defined as a string) to null, otherwise put the date in this field. Use that string value to feed into your datatset. e.g.

	if (reader->IsDBNull(4) == false)
	{
	    tmp_strValidTo = reader->GetDateTime(4).ToShortDateString();
        }
	else
	{	
	   tmp_strValidTo = nullptr;
	}
<pre>
 
Share this answer
 
Comments
sanomama 13-Jul-11 0:46am    
thanks :)
which version of SQL you are using?
 
Share this answer
 
Comments
sanomama 13-Jul-11 0:18am    
microsoft sql server 2008
Save date to database in normal DateTime format. And convert to Date.ToShortString() while binding to DataGridView column (or as you needed).
 
Share this answer
 
Assign the actual value to Nullable DateTime object. Then check HasValue and use it.

Example:

C#
DateTime? dateTime = [DB VALUE];

if (dateTime.HasValue)
{
    // use your logic to show dateTime.ToShortString();
}


Mark it as answer if it is helpful.
 
Share this answer
 

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