3
Answers

IndexOutOfRangeException problem with code

Photo of djcskinner

djcskinner

20y
4.5k
1
hi i have the follwoing code.... : : static int a = 0; static string fileLine = ""; static string[] fileLines = null; static int totalFileLength; static int loopLength; static int index2; : : public static void ParseFile(string FileName) { fileLines = FileReaderUtil.SplitFileByLine(FileName); totalFileLength = (fileLines.Length / 36); loopLength = (fileLines.Length / totalFileLength); newMethod(); } public static void newMethod() { do { for(index2 = a; index2 <= loopLength ; index2++) { fileLine = fileLines[index2].Trim(); if( fileLine.Length > 0 ) { pitch1 = fileLine.Substring(5, 1).Trim(); if (pitch1 == "O") { pitchTot++; } pitchArray[pitchNumb] = pitchTot; } } a += loopLength; loopLength+=loopLength; pitchNumb++; newMethod(); } while (index2 < (loopLength * totalFileLength)); printarray(); // goes off an prints out the pitchArray } so i want the file to be split into sections of 36 lines each, then to cycle through each set of these 36 lines until the end of the file is reached, incrementing the pitchTot integer if the charcter found is equal to "O", and then adding it to the pitchArray at index pitchNumb. if i take the do loop out and the call to newMethod() from newMethod() itself, i get the correct output for the zero index of the pitchArray, but then when i put it back in i get this error: Additional information: Index was outside the bounds of the array. so i dont know if this is easily solved (or if there id a much better way of doing it!) as i am just starting to learn programming, and so am after some help! any given would be greatly appreciated. thanks

Answers (3)

0
Photo of sham suman
NA 59 0 14y
Hi,

You can use Acceptchange() method after deleting code. Just check this.
Change the table name as per ur TableName.
Insted of session You can use direct table from DataAdapter. Just assign table name. Check this code...

Write this code in GridView1 RowDelete Command event.

protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {      
        int i;       
        DataSet objds = (DataSet)Session["ObjDataSet"];     //Assign table from database here.
        DataTable dtNewAct = (DataTable)objds.Tables[0];
        i = GridView1.SelectedIndex;
        for (int iRow = 0; iRow < objds.Tables[0].Rows.Count - 1; iRow++)
        {
            if (i == iRow)
            {
                if (objds.Tables[0].Rows[i].RowState != DataRowState.Deleted)
                {
                    objds.Tables[0].Rows[i].Delete();
                    objds.Tables[0].AcceptChanges();
                    Session["ObjDataSet"] = objds;
                }
            }
        }

    }

Accepted
0
Photo of Sam Hobbs
NA 28.7k 1.3m 14y
0
Photo of adam abdul
NA 8 0 14y
hai,
Thanks for kind response, i tried ur code but it fails to get table name.
Here is the code below

Note: adduser is my table name

  private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
            }

            addUserTableAdapter.Update(checkOutDataSet.Tables[adduser]);           
           
        }


error message:

Error    1    The name 'adduser' does not exist in the current context  
0
Photo of DRISHTY
NA 592 0 14y

Add following line after your delete logic :
sqllAdap.Update(ds.Tables[TBL_NAME]);