I have been working on asp.net 3.5.I want to Convert a DateTime Data from sqldatareader to a String on "dd-MM-yyyy" Format.
But when I use "dd-MM-yyyy" formatting parameter as "rdMonthlyLeave["LEAVE_DATE"].ToString("dd-MM-yyyy")" browser returns compile error as
Compiler Error Message: CS1501: No overload for method ‘ToString’ takes ‘1’ arguments
Do you have a solution?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You need to cast it to DateTime first:
DateTime leave = (DateTime) rdMonthlyLeave["LEAVE_DATE"];
DoSomethingWith(leave.ToString("dd-MM-yyyy"));
or just
((DateTime)rdMonthlyLeave["LEAVE_DATE"]).ToString("dd-MM-yyyy")
The return type of the DataReader indexer is just object, and object doesn’t have an overload of ToString which takes a string. Don’t forget that overloading is a compile-time decision – the compiler picks the appropriate method with a compatible signature, and only overriding occurs based on the execution-time type. In this case there is no overload of ToString with a compatible signature, so you get a compile-time error.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0