0

I have been getting a strange error during run time. Error Message

The Datatable is Below DataTable

I looked into my code and I specifically make it sure that this doesn't happen by using Rows.Count (for supportPointSelected). I check the Count and if and only of its lager than 0 (the First IF statement of the code) I go ahead with it. Please find the code below.

private List<byte> routeHandler(DataTable supportPointSelected, double taskState, int indices)
{
    //TPCANStatus statusCan = new TPCANStatus();
    int a = supportPointSelected.Columns.IndexOf("Number");     // column number indices
    int b = supportPointSelected.Columns.IndexOf("XSupport"); // column number X
    int c = supportPointSelected.Columns.IndexOf("YSupport"); // column number Y
    int d = supportPointSelected.Columns.IndexOf("VSupport"); // column number Velocity


    if (supportPointSelected.Rows.Count > 0) // Check to Avoid the Error
    {
        if (Convert.ToDouble(supportPointSelected.Rows[0][d]) == 0)
        {
            supportPointSelected.Rows[0][d] = 0.01; // This is the Point the Error Occurs
        }
        else { }
                }
    else { }

    List<byte> output = new List<byte>();
    // Logic do Something
    return output;
}
Abhishek Kumar
  • 342
  • 5
  • 22
  • @Soner Gönül The answer from other questions is not okay for me. Please read the code (The first if statement). I am saying the error shall not occur as I already handle the exception. – Abhishek Kumar Jan 25 '16 at 07:31
  • What is the value of `d` when the error occurs? What you are saying makes no sense to me. Can you make a small example with some code that fills in a datatable and passes it to the function. Please leave out the non relevant code. Other than that, you shouldn't be checking for equality on floating point numbers. – Jef Patat Jan 25 '16 at 09:40
  • Nevermind, I think I know the answer, change your question a bit so it can be answered. Most probably `d== -1` because the column is not found. The error is not on the rows, it's on the columns. – Jef Patat Jan 25 '16 at 09:46
  • @JefPatat The value of "d" at the point when Error occurs is 4 and not -1. I have checked that too. And, why shall not I be checking for equality on Floating points when the logic demands it? Is there any suggestion by Microsoft for not to do so or you come up with this based on your experience? – Abhishek Kumar Jan 25 '16 at 10:15
  • I guess you're better of making a small example then. You didn't post the datatable so it's guesswork as to what might be going wrong. Concerning the floating point issue: [here for example](http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm). It's not you cannot do it, it depends on your logic, but in general it's a bad idea. – Jef Patat Jan 25 '16 at 10:29
  • @JefPatat I have edited the question and added the DataTable Snapshot at the Point when It throws Error. One important point, this error does not occur every time (which makes it harder to get to the root cause) – Abhishek Kumar Jan 25 '16 at 10:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101577/discussion-between-jef-patat-and-abhishek-kumar). – Jef Patat Jan 25 '16 at 11:01

0 Answers0