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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…