Click here to Skip to main content
15,885,043 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using VS2010 and VB.net 2.0. I have modified code that I found online that will allow me to mass update a gridview. The datakey for the gridview is an operator number (referenced by variable currentID), most of which are 3 digits, but a few are 4 digits. The code works great except for when it comes to the rows with the 4 digit operator numbers. The 4 digit operator number returns an error of Index was outside the bounds of the array when it gets to this statement:

VB
Dim gvMgr_row As System.Data.DataRow = gvMgr_originalDataTable.Select(String.Format("op_num = {0}", currentID))(0)


I have been unable to determine why 4 digits cause an issue and would appreciate any help or advice. All of the relevant code should be included below.

VB
Protected Sub MgrUpdateButton_Click(sender As Object, e As System.EventArgs) Handles MgrUpdateButton.Click
    gvMgr_originalDataTable = CType(ViewState("gvMgr_originalValuesDataTable"), System.Data.DataTable)

    For Each r As GridViewRow In gvMgrData.Rows
        If gvMgr_IsRowModified(r) Then gvMgrData.UpdateRow(r.RowIndex, False)
    Next

    ' Rebind the Grid to repopulate the original values table.
    gvMgr_tableCopied = False
    gvMgrData.DataBind()
End Sub

Protected Function gvMgr_IsRowModified(ByVal r As GridViewRow) As Boolean
    Dim currentID As Integer
    Dim currentThreshold1 As String
    Dim currentPayout1 As String
    Dim currentThreshold2 As String
    Dim currentPayout2 As String
    Dim currentThreshold3 As String
    Dim currentPayout3 As String
    Dim currentThreshold4 As String
    Dim currentPayout4 As String

    currentID = Convert.ToInt32(gvMgrData.DataKeys(r.RowIndex).Value)

    currentThreshold1 = CType(r.FindControl("Threshold1TextBox"), TextBox).Text
    currentPayout1 = CType(r.FindControl("Payout1TextBox"), TextBox).Text
    currentThreshold2 = CType(r.FindControl("Threshold2TextBox"), TextBox).Text
    currentPayout2 = CType(r.FindControl("Payout2TextBox"), TextBox).Text
    currentThreshold3 = CType(r.FindControl("Threshold3TextBox"), TextBox).Text
    currentPayout3 = CType(r.FindControl("Payout3TextBox"), TextBox).Text
    currentThreshold4 = CType(r.FindControl("Threshold4TextBox"), TextBox).Text
    currentPayout4 = CType(r.FindControl("Payout4TextBox"), TextBox).Text

    Dim gvMgr_row As System.Data.DataRow = gvMgr_originalDataTable.Select(String.Format("op_num = {0}", currentID))(0)

    If Not currentThreshold1.Equals(gvMgr_row("Threshold1").ToString()) Then Return True
    If Not currentPayout1.Equals(gvMgr_row("Payout1").ToString()) Then Return True
    If Not currentThreshold2.Equals(gvMgr_row("Threshold2").ToString()) Then Return True
    If Not currentPayout2.Equals(gvMgr_row("Payout2").ToString()) Then Return True
    If Not currentThreshold3.Equals(gvMgr_row("Threshold3").ToString()) Then Return True
    If Not currentPayout3.Equals(gvMgr_row("Payout3").ToString()) Then Return True
    If Not currentThreshold4.Equals(gvMgr_row("Threshold4").ToString()) Then Return True
    If Not currentPayout4.Equals(gvMgr_row("Payout4").ToString()) Then Return True

    Return False
End Function
Posted
Comments
Sergey Alexandrovich Kryukov 17-May-13 16:44pm    
In what lime? Use the debugger, such bugs are very easy to fix.
The code is very bad though, all hard-coded. This is not programming at all, just messing around. Write the code accurately, many bugs will go by themselves...
—SA
cherit 17-May-13 17:32pm    
There are many different skill levels and obviously I am a beginner. I have been using the debugger and have not been able to find the answer, which is why I posted my question.
TnTinMn 17-May-13 20:52pm    
"The 4 digit operator number returns an error of Index was outside the bounds of the array when it gets to this statement:

Dim gvMgr_row As System.Data.DataRow = gvMgr_originalDataTable.Select(String.Format("op_num = {0}", currentID))(0)"

The reason for the index out of bounds error is that this select statement is not returning any rows. You need to determine the reason for this.

Since currentID is computed:

currentID = Convert.ToInt32(gvMgrData.DataKeys(r.RowIndex).Value)

Verify that currentID is what you expect it to be.

Change: Dim gvMgr_row As System.Data.DataRow = gvMgr_originalDataTable.Select(String.Format("op_num = {0}", currentID))(0)

To:

Dim gvMgr_row As System.Data.DataRow

Try
gvMgr_row=gvMgr_originalDataTable.Select(String.Format("op_num = {0}", currentID))(0)
Catch ex As Exception
dim cid as object = gvMgrData.DataKeys(r.RowIndex).Value
Stop
End Try

This will stop the debugger and allow you to inspect the variables to see what is going on by hovering the mouse pointer over the variable name.

The above will not solve your problem, but it will allow you to investigate possible causes.
cherit 20-May-13 12:33pm    
Thank you. I have put the try in place. Out of the first three rows, 2 of them have a 4 digit ID, but only one of them is caught by the catch statement. I verified the indexes are within range on both. I also verified the data using the visualizer. The cid value is 2270 which is the op_num for row index 2. I have also verified that the gvMgr_originalDataTable also has op_num of 2270.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900