0

Recently, I've been getting an IndexOutOfRange exception in a particular method. The new code in this function reads a "csv" file (a .txt file renamed with the extension "CSV") and parses it; so it must be code specific to that or else the data itself that is raising this exception.

But it's apparently not on the Insert into the database, because I added a MessageBox.Show() in the catch block where the insert takes place, and I never see it.

public bool PopulatePlatypusItemsListAndInsertIntoPlatypusItemsTable(frmCentral fc)
{
    const int Platypus_ID_OFFSET = 0;
    const int Platypus_ITEM_ID_OFFSET = 1;
    const int ITEM_ID_OFFSET = 2;
    const int PACKSIZE_OFFSET = 3;

    bool ret = false;
    try
    {
        string dSQL;

        bool First = true;

        if (File.Exists(csvFilePathName))
        {
            int fzz = 0;

            dSQL = "DELETE FROM PlatypusItems";
            try
            {
                dbconn.DBCommand(dSQL, true);
            }
            catch
            {
                frmCentral.listboxMessage.TopIndex = frmCentral.listboxMessage.Items.Add(Convert.ToString(++frmCentral.lstMessageCount) +
                                                                                         ". Error processing PlatypusItem data from server");
            }

            SqlCeConnection conn = dbconn.GetConnection();
            if (conn != null && conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            SqlCeCommand cmd = conn.CreateCommand();

            cmd.CommandText = "INSERT INTO PlatypusItems ( PlatypusID, PlatypusItemID, ItemID, PackSize) VALUES (?, ?, ?, ?)";

            if (!ret) 
            {
                ret = true;
            }

            PlatypusItem DuckbillItm = new PlatypusItem();
            string thisLine;
            string[] arrLine;
            using (StreamReader sr = new StreamReader(csvFilePathName)) 
            {
                while (sr.Peek() >= 0) 
                {
                    thisLine = sr.ReadLine();
                    arrLine = thisLine.Split(',');
                    DuckbillItm.PlatypusID = arrLine[Platypus_ID_OFFSET];
                    DuckbillItm.PlatypusItemID = arrLine[Platypus_ITEM_ID_OFFSET];
                    DuckbillItm.ItemID = arrLine[ITEM_ID_OFFSET];
                    DuckbillItm.PackSize = Convert.ToInt32(arrLine[PACKSIZE_OFFSET]);

                    PlatypusItemList.List.Add(DuckbillItm);

                    dSQL = "INSERT INTO PlatypusItems (PlatypusID, PlatypusItemID, ItemID, PackSize) VALUES (" + DuckbillItm.PlatypusID + ",'" +
                        DuckbillItm.PlatypusItemID + "','" + DuckbillItm.ItemID + "'," + DuckbillItm.PackSize + ")";

                    if (!First)
                    {
                        cmd.Parameters[0].Value = DuckbillItm.PlatypusID;
                        cmd.Parameters[1].Value = DuckbillItm.PlatypusItemID;
                        cmd.Parameters[2].Value = DuckbillItm.ItemID;
                        cmd.Parameters[3].Value = DuckbillItm.PackSize.ToString();
                    }

                    if (First)
                    {
                        cmd.Parameters.Add("@PlatypusID", DuckbillItm.PlatypusID);
                        cmd.Parameters.Add("@PlatypusItemID", DuckbillItm.PlatypusItemID);
                        cmd.Parameters.Add("@ItemID", DuckbillItm.ItemID);
                        cmd.Parameters.Add("@PackSize", DuckbillItm.PackSize);
                        cmd.Prepare();
                        First = false;
                    }

                    if (frmCentral.CancelFetchInvDataInProgress)
                    {
                        return false;
                    }

                    try
                    {
                        // testing with these reversed: - either way, get the IndexOutOfRange exception...
                        //dbconn.DBCommand(cmd, dSQL, true);
                        dbconn.DBCommand(cmd, cmd.CommandText, true); //<-- If this works as well or better, dSQL is only there for the progress updating code below
                        // the first line is the legacy code; the second seems more sensible to me; both seem to work
                    }
                    catch (Exception x)
                    {
                        MessageBox.Show(string.Format("dbcommand exc message = {0}; PlatypusID = {1}; PlatypusItemID = {2}; ItemID = {3}; PackSize = {4}",      

                    x.Message, DuckbillItm.PlatypusID, DuckbillItm.PlatypusItemID, DuckbillItm.ItemID, DuckbillItm.PackSize));//TODO: Remove
                        frmCentral.listboxMessage.TopIndex =
                            frmCentral.listboxMessage.Items.Add(Convert.ToString(++frmCentral.lstMessageCount) +
                            ". Error processing Platypus Item data from server");
                    }

                    fzz += dSQL.Length; //<-- tried commenting this weird code out, but still get IndexOutOfRangeException

                    if (fzz > fc.ProgressChangedIndex)
                    {
                        fc.ProgressChangedIndex = fzz + fc.ProgressChangedIncrement;
                        if (((frmCentral.ProgressBar.progressBar1.Maximum/4) + (fzz*3) < frmCentral.ProgressBar.progressBar1.Maximum) &&
                            ((frmCentral.ProgressBar.progressBar1.Maximum/4) + (fzz*3) > frmCentral.ProgressBar.progressBar1.Value))
                        {
                            frmCentral.ProgressBar.progressBar1.Value = (frmCentral.ProgressBar.progressBar1.Maximum/4) + (fzz*3);
                            frmCentral.ProgressBar.progressBar1.Refresh();
                        }
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        duckbilledPlatypiRUs.ExceptionHandler(ex, "PlatypusItemFile.PopulatePlatypusItemsListAndInsertIntoPlatypusItemsTable");
    }
    return ret;
}

I know this code is kind of a wacko mix of different styles; the good code is mine, and the weird code is legacy (g,d&r)

I can do this, of course, to sweep the dust under the rug:

catch (Exception ex)
{
    if (ex.Message.IndexOf("IndexOutOfRange") < 0)
    {
        duckbilledPlatypiRUs.ExceptionHandler(ex, "PlatypusItemFile.PopulatePlatypusItemsListAndInsertIntoPlatypusItemsTable"); 
    }
}

...but I don't know if that IndexOutOfRangeException is actually something serious that is wreaking mayhem in the innards of this app.

UPDATE

I added this code:

if (arrLine[PLATYPUS_ID_OFFSET].Length > 10) //TODO: Remove?
            {
                                MessageBox.Show(string.Format("PLATYPUS_ID_OFFSET length should be 10; was {0}", arrLine[PLATYPUS_ID_OFFSET].Length));
                                arrLine[PLATYPUS_ID_OFFSET] = arrLine[PLATYPUS_ID_OFFSET].Substring(0, 10);
                            }
                            if (arrLine[PLATYPUS_ITEM_ID_OFFSET].Length > 19)
                            {
                                MessageBox.Show(string.Format("PLATYPUS_ITEM_ID_OFFSET length should be 19; was {0}", arrLine[PLATYPUS_ID_OFFSET].Length));
                                arrLine[PLATYPUS_ITEM_ID_OFFSET] = arrLine[PLATYPUS_ITEM_ID_OFFSET].Substring(0, 19);
                            }
                            if (arrLine[ITEM_ID_OFFSET].Length > 19)
                            {
                                MessageBox.Show(string.Format("ITEM_ID_OFFSET length should be 19; was {0}", arrLine[PLATYPUS_ID_OFFSET].Length));
                                arrLine[ITEM_ID_OFFSET] = arrLine[ITEM_ID_OFFSET].Substring(0, 19);
                            }

...and I never saw those MessageBox.Show()s, so I guess it's not the three string values that are causing the problem; perhaps the int (PackSize). PackSize would never be greater than what Int32 allows; is there a TryParse() equivalent in .NET 1.1?

UPDATE 2

I do see the "IndexOutOfRange" from the exception here, and yet never the MessageBox.Show()s:

try
{
    thisLine = sr.ReadLine();
    arrLine = thisLine.Split(',');
    if (arrLine[PLATYPUS_ID_OFFSET].Length > 10) //TODO: Remove?
    {
        MessageBox.Show(string.Format("PLATYPUS_ID_OFFSET length should be 10; was {0}", arrLine[PLATYPUS_ID_OFFSET].Length));
        arrLine[PLATYPUS_ID_OFFSET] = arrLine[PLATYPUS_ID_OFFSET].Substring(0, 10);
    }
    . . .
    DuckbillItm.PLATYPUSID = arrLine[PLATYPUS_ID_OFFSET];
    DuckbillItm.PLATYPUSItemID = arrLine[PLATYPUS_ITEM_ID_OFFSET];
    DuckbillItm.ItemID = arrLine[ITEM_ID_OFFSET];
    DuckbillItm.PackSize = Convert.ToInt32(arrLine[PACKSIZE_OFFSET]);
}
catch (Exception exc)
{
    MessageBox.Show(exc.Message);
}

UPDATE 3

It was bad data, after all; some of the lines had four commas in them instead of the expected three. So, I just removed those lines (after adding code to check each element of the array for size, and trimming them prior to that) and it runs fine.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    You're not stepping through to see precisely where it occurs? – DonBoitnott May 29 '13 at 18:18
  • 1
    Have you tried checking the values of all your command parameters to make sure they are as expected? – Andrew May 29 '13 at 18:19
  • @DonBoitnott: Can't step through - this is a Windows CE app for which I cannot use emulators. I have to use VS2003 and .NET1.1, etc. It's a pain; and that's an understatement. I am reliant on MessageBox.Show()s to debug state. – B. Clay Shannon-B. Crow Raven May 29 '13 at 18:23
  • @Andrew: I guess I'll have to do that; but it's going to mean mashing the OK button a gazillion times and going all google-eyed watching the values pop up. – B. Clay Shannon-B. Crow Raven May 29 '13 at 18:24
  • 1
    Well poo. I only see two places where indexing occurs. First, reading the CSV... if you get a line with less than 4 items (defined as separated by comma) you'll be asking for more than exists in the array. Second, the command parameters...where are they instantiated? Are there really 4? – DonBoitnott May 29 '13 at 18:25
  • Yeah that "sweep under the rug" thing is a bad idea. Best to handle it proper. – DonBoitnott May 29 '13 at 18:27
  • If you put your try/catch's in the right place, you will only have to look at the values in scope at the time the exception occurs to find out what the data is, you don't have to step through each record. If your try catch is not in the right place, then refactor it. – David C May 29 '13 at 18:29
  • @DonBoitnott: Yes, there are four columns in the table, and four members in the corrresponding class. I think I'll check them for length on each assignment; perhaps that's the problem - one of the vals is longer than the underlying column length. – B. Clay Shannon-B. Crow Raven May 29 '13 at 18:32
  • @ClayShannon Not that that's a bad idea, but I'd expect a different exception if that were the problem. IndexOutOfRange is typically reserved for sizes of collections, such as an array. Asking for myArray[4] where only 4 items exist, etc. – DonBoitnott May 29 '13 at 18:34
  • You could try wrapping the section that Splits the incoming CSV line in its own Try/Catch(Exception ex) and looking there for an exception (MessageBox.Show(ex.Message), for instance). – DonBoitnott May 29 '13 at 18:37

1 Answers1

0

My initial guess: Probably one of the arrLine[...] lines (e.g. DuckbillItm.PlatypusID = arrLine[Platypus_ID_OFFSET]) would cause this if your input .csv file is bad. Do a debug build and put your breakpoint on the catch handler, and tell us what "ex.StackTrace" says.

VeeTheSecond
  • 3,086
  • 3
  • 20
  • 16