1

i am getting the Error When the value of row[colname]="NULL" and "ABC" is there any way to avoid it !!
i even try to Try parse but i am getting error cannot convert object to string in row[colname](commented the code) error cannot convert object to string in row[colname]

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

foreach (DataRow row in csvData.Rows)
            {
                //var Result = double.TryParse(row[colname], out myDec);

                if (Convert.ToDouble(row[colname]) >= med1 && Convert.ToDouble(row[colname]) <= med2)
                {
                    al.Add(Convert.ToDouble(row[colname]));
                    //Console.WriteLine("value greater than 20 % median= {0}", Convert.ToDouble(row["Data Value"]));
                }
1
  • You must always assume that the file contains bad text, so always favor TryParse() so you can generate a decent error message. If the column contains "ABC" or "NULL" then there is of course no hope of ever converting it correctly. Get in touch with whomever wrote the code that generated the file so this issue can be hammered out. You will not find him here. Commented Mar 27, 2018 at 9:00

2 Answers 2

1

You problem occurs because you are trying to convert "NULL" and "ABC" to a double type, that is simply not possible. Try this instead:

double myDec;
foreach (DataRow row in csvData.Rows)
{
      double.TryParse(row[colname]?.ToString(), out myDec)
      if (myDec >= med1 && myDec <= med2)
      {
           al.Add(myDec);
      }

Use row[colname]?.ToString() to convert row[colname] from a object to a string. double.TryParse(String, Double) requires the first parameter to be a string not an object.

Sign up to request clarification or add additional context in comments.

4 Comments

getting error cannot convert object to string 'row[colname]
@nikhiljain I updated it! I added row[colname].ToString()
@nikhiljain Have you tried my answer it should work now
@nikhiljain I updated my question. I changed row[colname].ToString() to row[colname]?.ToString() to avoid a NullReferenceException.
0

A quick question: Why the line var Result = double.TryParse(row[colname], out myDec); is commented? that was the right way to do this. You are getting that exception just because of Convert.To() failed to convert the value in row[colname], when double.TryParse() came into that role, you can check the return value for conversion status and make use of the out value in the out variable; The code would be like this:

foreach (DataRow row in csvData.Rows)
{
    double myDec;

    if (row[colname] != null && double.TryParse(row[colname].Tostring(), out myDec))
    {
        if (myDec >= med1 && myDec <= med2)
        {
            al.Add(myDec);                      
        }
    }
}

Please note : include row[colname] !== DbNull.Value if there any chance for getting DbNull in row[colname]

2 Comments

still getting error cannot convert object to string 'row[colname]
@nikhiljain: check the updates in my post, change the if to : if (row[colname] != null && double.TryParse(row[colname].Tostring(), out myDec))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.