Govinda Rajbhar
Govinda Rajbhar

Reputation: 3034

Error when converting datetime to string using dd/MM/yyyy format

My Code :

This line working properly.

txtjoindate.Text = user.Join_Date.ToString("dd/MM/yyyy"); 

And this line giving me error.

txtEnddate.Text = user.End_Date.ToString("dd/MM/yyyy");

Error:

no overload for method tostring takes 1 arguments

Edit:

Here Join_Date and End_Date are the column names of userDetails table which have a data type DateTime.

So please make me understand why it is happening both fields have a same data type still in my first line code it working fine and second line is not working and giving me error as above.

So If somebody have idea about it so please help me.

Upvotes: 1

Views: 616

Answers (1)

Marko Juvančič
Marko Juvančič

Reputation: 5890

Is End_Date nullable (i.e. DateTime?)?

Then you should do this:

 txtEnddate.Text = user.End_Date.HasValue 
    ? user.End_Date.Value.ToString("dd/MM/yyyy") 
    : "/";

EDIT: better handling, tnx to the commenters.

Upvotes: 7

Related Questions