Click here to Skip to main content
15,881,669 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
when I tried to compare two lines, it shows error like this:

CSS
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:

SQL
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()))



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:

C#
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;

                  }
Posted
Updated 28-Aug-12 2:43am
v3
Comments
Legor 28-Aug-12 8:03am    
What does the String look like?
Sarrrva 28-Aug-12 8:09am    
can you show more code with table structure?
regards
sarva
pramod.hegde 28-Aug-12 8:10am    
Did you try to check what does 'dt.Rows[i + 1][1].ToString()' return? Check whether it contains any Boolean value OR String OR DateTime (it fails in all these cases).
jessicachen12 28-Aug-12 8:34am    
how to check the value of 'dt.Rows[i + 1][1].ToString()' ? I tried to debug, but it just doesn't work...well, there were three different people who wrote this code...however, in an other page, I use the same data and the same methode to read and it works...

the problem is I can't check the value by debuging :( horibble!!
Santhosh Kumar Jayaraman 28-Aug-12 8:44am    
Check my solution

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
 
Share this answer
 
v2
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.
 
Share this answer
 
Comments
Santhosh Kumar Jayaraman 28-Aug-12 8:28am    
why so?
jessicachen12 28-Aug-12 8:35am    
how to check the value of 'dt.Rows[i + 1][1].ToString()' ? I tried to debug, but it just doesn't work...well, there were three different people who wrote this code...however, in an other page, I use the same data and the same methode to read and it works...

the problem is I can't check the value by debuging :( horibble!!
It means your string is not in double format. when it tried to convert into double, it couldnt and threw error.


Use Double.TryParse.
C#
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 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
 
Share this answer
 
v5
Comments
jessicachen12 28-Aug-12 8:45am    
hi, thank you for answering. what do d1 d2 and d3 mean? sorry, I don't fully understand the TryParse method :S
Santhosh Kumar Jayaraman 28-Aug-12 8:45am    
d1, d2 , d3 are local variables,
Santhosh Kumar Jayaraman 28-Aug-12 8:47am    
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 double.MinValue same as almost equal to 0 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
Legor 28-Aug-12 8:53am    
But this isn't really a solution to his problem. Just because the program doesn't crash it won't be solved.
Santhosh Kumar Jayaraman 28-Aug-12 8:56am    
It will still execute if block if values are same.
If values are in double and dint match, if loop wont be executed.
If its not able to parse, it means incorrect and it will return

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900