Getting error of IndexOutOfRangeException in C# RRS feed

  • Question

  • I am getting error of IndexOutOfRangeException in the following code.

     public static string parseVidPid()
                DataSet myDs = new DataSet();

                // Get a FileStream object.
                FileStream myFs = new FileStream(PidVidPath, FileMode.Open, FileAccess.Read);

                // Apply the ReadXml(fileStream method) to read the file

                DataTable myDt = myDs.Tables[0];

                int no = myDt.Rows.Count;
                String[] nodeVal = new String[no];

                for (int i = 0; i < myDt.Rows.Count; i++)
                    nodeVal[i] = myDt.Rows[i].ToString();

                    Console.WriteLine("Inside parseVidPid- Row = " + nodeVal[i]);


                return nodeVal[no];

    Where XML file is having 2 nodes. can anybody tell me where is the issue?

    Friday, July 13, 2012 2:29 PM


  • Take to a look to your return statement:

    return nodeVal[no];

    You're trying to return the element at no position, that is equal to myDt.Rows.Count. As array index as 0-based, this value isn't correct (it should be nodeVal[no - 1]).

    Marco Minerva [MCPD]
    Twitter: @marcominerva

    • Proposed as answer by Norkk Friday, July 13, 2012 3:18 PM
    • Marked as answer by Lisa Zhu Tuesday, July 17, 2012 8:33 AM
    Friday, July 13, 2012 3:13 PM