Domanda

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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top