无过载的方法和QUOT;&的ToString QUOT;需要1参数 [英] no overload for method "ToString" takes 1 arguments

查看:150
本文介绍了无过载的方法和QUOT;&的ToString QUOT;需要1参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据读取器从数据库中读取DATAS。
我读从销售表TotalPrice。
我想显示的总价格为2小数位。

I have a data reader to read the datas from database. I am reading TotalPrice from sales table. I would like to show the total price as 2 decimal place.

在code是一些链接:

The code is something link that:

TotalPrice.Text = myReader["TotalPrice"].ToString("N2");

不过,我中遇到这个错误:没有过载的方法的ToString需要1个参数
什么是错的code?

However i encounted this error: no overload for method "ToString" takes 1 arguments What's wrong with the code?

推荐答案

假设TotalPrice是一个十进制列,这是myReader一个DataReader:

Assuming that TotalPrice is a Decimal column, and that myReader is a DataReader:

TotalPrice.Text = myReader.GetDecimal(myReader.GetOrdinal("TotalPrice")).ToString("N2");

这里的想法是,myReader的索引(你打电话时使用的是什么 myReader [TotalPrice] )返回一个对象。它具有的,因为它不知道在编译时该列是什么类型。通过使用 .GetDecimal(),你保证你得到一个十进制值回,或错误。

The idea here is that myReader's indexer (what you are using when you call myReader["TotalPrice"]) returns an Object. It has to, since it doesn't know at compile time what type the column is. By using .GetDecimal(), you are assuring that you get a Decimal value back, or an error.

的ToString 重载需要一个格式字符串宣布小数,而不是对象。对象的的ToString 不带任何参数。

The .ToString overload that takes a format string is declared on Decimal, not on Object. Object's .ToString takes no arguments.

要GetOrdinal内调用返回现场的指数给出,如果您在多个数据行的循环这样的名称 - ,你可能会想缓存在变量中。

The inner call to GetOrdinal returns the field's index given the name- if you are doing this in a loop for multiple data rows, you'd probably want to cache that in a variable.

这篇关于无过载的方法和QUOT;&的ToString QUOT;需要1参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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