'No overload for method 'ToString' takes '1' arguments' error occur While Formatting Datetime Field To String ON “dd-MM-yyyy” format

笑着哭i 提交于 2019-11-29 01:11:21

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!