“无过载方法'的ToString'采取'1'参数'错误发生在格式化日期时间字段为字符串ON" DD-MM-YYYY"格式 [英] 'No overload for method 'ToString' takes '1' arguments' error occur While Formating Datetime Field To String ON "dd-MM-yyyy" format

查看:405
本文介绍了“无过载方法'的ToString'采取'1'参数'错误发生在格式化日期时间字段为字符串ON" DD-MM-YYYY"格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在asp.net 3.5.I想从SqlDataReader的转换datetime数据为String的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 第一:

You need to cast it to DateTime first:

DateTime leave = (DateTime) rdMonthlyLeave["LEAVE_DATE"];
DoSomethingWith(leave.ToString("dd-MM-yyyy"));

或只是

((DateTime)rdMonthlyLeave["LEAVE_DATE"]).ToString("dd-MM-yyyy")

DataReader的索引器的返回类型就是对象对象没有过载的ToString 这需要一个字符串。不要忘记的重载的是一个编译时的决定 - 编译器选取具有兼容签名适当的方法,也是唯一的覆盖的发生根据执行时的类型。在这种情况下出现的的任何的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.

这篇关于“无过载方法'的ToString'采取'1'参数'错误发生在格式化日期时间字段为字符串ON" DD-MM-YYYY"格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆