输入的字符串格式不正确Convert.ToDouble(dt.Rows [i] [j] .ToString()) [英] Input string was not in a correct format Convert.ToDouble(dt.Rows[i][j].ToString())

查看:76
本文介绍了输入的字符串格式不正确Convert.ToDouble(dt.Rows [i] [j] .ToString())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试比较两行时,它显示如下错误:

when I tried to compare two lines, it shows error like this:

Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.



我的代码是:



My code is:

if (i != (dt.Rows.Count - 1))
{
    if (Convert.ToDouble(dt.Rows[i][1].ToString()) ==
        Convert.ToDouble(dt.Rows[i + 1][1].ToString())) // this is the wrong line
    {
        for (int j = 0; j < dt.Rows.Count - i; j++)
        {
            if (Convert.ToDouble(dt.Rows[i][1].ToString()) ==
                Convert.ToDouble(dt.Rows[i + j][1].ToString()))




我也尝试过将ToDouble更改为ToInt32,但这根本不起作用.

谁能告诉我,此错误消息是什么意思?

非常感谢!
更多代码,则dt.Rows [i] [j]是一些(超链接)数据绑定:




I''ve tried also to change the ToDouble to ToInt32, but it just doesn''t work.

could anyone tell me, what does this error message mean?

many thanks!!!

more codes, the dt.Rows[i][j] is something (hyperlink) databind:

for (int i = 0; i < dt.Rows.Count; i++) {

              switch (dt.Rows[i][0].ToString()) {

                  case "ENTITLEMENT":
                      {
                          if (dt.Rows[i][13].ToString() != "" && dt.Rows[i][14].ToString() != "")
                          {
                              order1 = order1 + Convert.ToDouble(dt.Rows[i][14].ToString());

                              

                              double X = 0;
                              double Y = 0;
                                      if (i != (dt.Rows.Count - 1))
                                      {
                                          if (Convert.ToDouble(dt.Rows[i][1].ToString()) == Convert.ToDouble(dt.Rows[i+1][1].ToString()))
                                          {
                                              for (int j = 0; j < dt.Rows.Count - i; j++)
                                              {
                                                  if (Convert.ToDouble(dt.Rows[i][1].ToString()) == Convert.ToDouble(dt.Rows[i + j][1].ToString()))
                                                  {
                                                      X = X + Convert.ToDouble(dt.Rows[i + j][14].ToString());
                                                      Y = Y + Convert.ToDouble(dt.Rows[i + j][13].ToString());
                                                      

                                                  }
                                                  else
                                                  {
                                                      i = i + j - 1;
                                                      break;
                                                  }
                                              }
                                              if (X <= Y)
                                              {
                                                  Deliver1 = Deliver1 + X;
                                                  X = 0; Y = 0;
                                              }
                                              else
                                              {
                                                  Deliver1 = Deliver1 + Y;
                                                  X = 0; Y = 0;
                                              }
                                          }

                                          else
                                          {
                                              if (Convert.ToDouble(dt.Rows[i][13].ToString()) > Convert.ToDouble(dt.Rows[i][14].ToString()))
                                              {
                                                  Deliver1 = Deliver1 + Convert.ToDouble(dt.Rows[i][14].ToString());
                                              }
                                              else
                                                  Deliver1 = Deliver1 + Convert.ToDouble(dt.Rows[i][13].ToString());
                                          }
                                      }
                                      else if (i == (dt.Rows.Count - 1) && i > 0 && Convert.ToDouble(dt.Rows[i][1].ToString()) != Convert.ToDouble(dt.Rows[i - 1][1].ToString()))
                                      {
                                          if (Convert.ToDouble(dt.Rows[i][13].ToString()) > Convert.ToDouble(dt.Rows[i][14].ToString()))
                                          {
                                              Deliver1 = Deliver1 + Convert.ToDouble(dt.Rows[i][14].ToString());
                                          }
                                          else
                                              Deliver1 = Deliver1 + Convert.ToDouble(dt.Rows[i][13].ToString());
                                      }
                                      else if (i == (dt.Rows.Count - 1) && i == 0)
                                      {
                                          if (Convert.ToDouble(dt.Rows[i][13].ToString()) > Convert.ToDouble(dt.Rows[i][14].ToString()))
                                          {
                                              Deliver1 = Deliver1 + Convert.ToDouble(dt.Rows[i][14].ToString());
                                          }
                                          else
                                              Deliver1 = Deliver1 + Convert.ToDouble(dt.Rows[i][13].ToString());
                                      }


                          }
                              break;

                  }

推荐答案

此错误表示:
无法将数据转换为其他字符串类型.
意思是:在某些情况下,"dt.Rows [i + 1] [1] .ToString()"返回的某些内容无法转换为double.
请使用调试器返回"dt.Rows [i + 1] [1] .ToString()"的内容.它不应返回布尔值或日期时间,这样就不能将其转换为double.

请检查
This error means:
It unable to convert the data into string type to other.
means:in some cases "dt.Rows[i + 1][1].ToString()" is returning some thing which can''t convert to double.
Please what is returning "dt.Rows[i + 1][1].ToString()" using debugger. it should not return Boolean or date time so that it can''t be converted to double.

Please Check


不能将dt.Rows[i][1].ToString()dt.Rows[i + 1][1].ToString()(或两者)都转换为double(或Int32).
也就是说,它不包含数字的有效表示形式.
您应该调试应用程序以检查此类变量的值.
Either dt.Rows[i][1].ToString() or dt.Rows[i + 1][1].ToString() (or both) cannot be converted to double (or to Int32).
That is it does not contain a valid representation of a number.
You should debug your application to check the values of such variables.


这意味着您的字符串不是双精度格式.当它尝试转换为double时,无法抛出错误.


使用Double.TryParse.
It means your string is not in double format. when it tried to convert into double, it couldnt and threw error.


Use Double.TryParse.
double d1;
double d2;
double d3;
if(!(Double.TryParse(dt.Rows[i][1].ToString(),out d1)))
return;
if(!(Double.TryParse(dt.Rows[i+1][1].ToString(),out d2)))
return;
if (d1==d2)
{
for (int j = 0; j < dt.Rows.Count - i; j++)
        {
Double.TryParse(dt.Rows[i + j][1].ToString(),out d3);
            if (d1==d3)
{
....



仅当字符串包含双精度值时,Tryparse才会将字符串解析为双精度.如果它不能转换,它不会抛出错误并停止您的程序.取而代之的是,如果我尝试以正常方式将字符串"s"转换为双精度值,它将设置值为0.00,这将引发您所说的错误.但是,如果我使用tryparse,它将返回我double.MinValue



Tryparse will parse the string to double only if the string contains double value. If its not able to convert, it wont threw error and stop your program. Instead of that it will set value as 0.00 if i try to convert string "s" to double in normal way, it will threw error as you said. But if i use tryparse, it will return me double.MinValue


这篇关于输入的字符串格式不正确Convert.ToDouble(dt.Rows [i] [j] .ToString())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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