none
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
                myDs.ReadXml(myFs);

                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]);
                }

                myFs.Close();

                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

Answers

  • 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]
    Blog: http://blogs.ugidotnet.org/marcom
    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