在将日期时间字段格式化为字符串ON时,发生“方法'ToString'不重载为'1'参数"错误.格式 [英] 'No overload for method 'ToString' takes '1' arguments' error occur While Formatting Datetime Field To String ON "dd-MM-yyyy" format
问题描述
我一直在asp.net 3.5上工作.我想将DateTime数据从sqldatareader转换为"dd-MM-yyyy"
格式的字符串.
但是,当我使用"dd-MM-yyyy"
格式参数作为"rdMonthlyLeave["LEAVE_DATE"].ToString("dd-MM-yyyy")"
时,浏览器将返回编译错误为
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
编译器错误消息:CS1501:方法'ToString'的重载没有接受'1'参数
Compiler Error Message: CS1501: No overload for method 'ToString' takes '1' arguments
您有解决方案吗?
推荐答案
您需要先将其转换为DateTime
:
DateTime leave = (DateTime) rdMonthlyLeave["LEAVE_DATE"];
DoSomethingWith(leave.ToString("dd-MM-yyyy"));
或者只是
((DateTime)rdMonthlyLeave["LEAVE_DATE"]).ToString("dd-MM-yyyy")
DataReader索引器的返回类型仅为object
,而object
没有具有字符串的ToString
重载.不要忘记 overloading 是编译时的决定-编译器选择具有兼容签名的适当方法,并且只有 overrideing 会根据执行时类型发生.在这种情况下, 不会包含具有兼容签名的ToString
重载,因此会出现编译时错误.
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.
这篇关于在将日期时间字段格式化为字符串ON时,发生“方法'ToString'不重载为'1'参数"错误.格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!