• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

Index was out of range

Hi,

  I am getting the following error and i wonder if you can fix with error please. I am posting the method, line no where the error occured  with a error message.

Error Message: {"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"}

Error in the following Line :-

Dim Description As String = AlarmsGrid.Rows(e.NewEditIndex).Cells(2).Text


Method used:-


  Protected Sub AlarmGrid_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
        Dim AlarmsGrid As GridView = New GridView
        AlarmsGrid.EditIndex = e.NewEditIndex
        'Dim plantCommand As SqlCommand
        Dim Description As String = AlarmsGrid.Rows(e.NewEditIndex).Cells(2).Text

        Dim mode As String = AlarmsGrid.Rows(e.NewEditIndex).Cells(3).Text
        Dim active As String = AlarmsGrid.Rows(e.NewEditIndex).Cells(4).Text
        Dim regulatory_alarm As String = AlarmsGrid.Rows(e.NewEditIndex).Cells(5).Text
        Dim auto_acknowledge As String = AlarmsGrid.Rows(e.NewEditIndex).Cells(6).Text
        AlarmsGrid.DataSource = AlarmModification
        AlarmsGrid.DataBind()
        FilterDataBind()
    End Sub


I am trying to edit the grid view using the above method .

Thanks,

Sreekanth Nagabandi.

0
Sreekanth_Nagabandi
Asked:
Sreekanth_Nagabandi
  • 3
  • 2
  • 2
  • +1
1 Solution
 
VoteyDiscipleCommented:
Well, first, which index is out of range?  e.NewEditIndex?  Or 2?

I'd suggest using the debugger to figure out what data you really have and what's being requested.
0
 
Sreekanth_NagabandiAuthor Commented:
Hi,

  I am getting the value of e.NewEditIndex value as 0 while debugging , not the Cells(2) value 0.

Thanks,
Sreekanth
0
 
boy8964Commented:
make sure e.NewEditIndex is not less then 0.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Sreekanth_NagabandiAuthor Commented:
Hai,

  I am new to thid grid view, I didn't get you exactly . How should i ensure  e.NewEditIndex value is not less than 0.

Thanks,

Sreekanth
0
 
DjDezmondCommented:
Is the user selecting the row/cell to edit? It looks as if the "EditIndex" does not exist, so you may be calling the wrong property.

Try using:
   Dim Description As String = AlarmsGrid.SelectedRows(0).Cells(2).value

SelectedRows(0) = The 1st selected row you have selected. (if multiple rows are selected, the 1st one will be used)
0
 
VoteyDiscipleCommented:
I haven't used this control in a while, but I'm guessing if e.NewEditIndex comes in as zero then the user isn't editing anything.

You may want to put at the beginning of your event handler:

If (e.NewEditIndex < 0) Then Return
0
 
Sreekanth_NagabandiAuthor Commented:
Hi,

  Let me explain the functionality. I am trying to edit a row in the grid view . when i click on the edit button . it goes from the edit mode to update mode , where i can update the changes or cancel those changes. I have achieved this by changing the editindex to 1 in the properties of the grid view. With this I can only edit the second row , but i want to edit all the rows in the grid view.

Then in the code behind , i have included these event methods , called rowediting and rowupdating which are invoked when the edit and update buttons are  clicked .
 But you know , it doesn't read the columns in  the row edit method as
 e.NewEditIndex = 0  , which i have mentioned in the earlier email.

Can you explain how can i achieve my functionality.
I will post the methods used below.



Protected Sub AlarmGrid_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
        Dim AlarmsGrid As GridView = New GridView
        AlarmsGrid.EditIndex = e.NewEditIndex = 1
        'Dim plantCommand As SqlCommand
        If e.NewEditIndex <= 0 Then
            Return
        Else


            Dim Description As String = AlarmsGrid.Rows(e.NewEditIndex).Cells(2).Text

            Dim mode As String = AlarmsGrid.Rows(e.NewEditIndex).Cells(3).Text
            Dim active As String = AlarmsGrid.Rows(e.NewEditIndex).Cells(4).Text
            Dim regulatory_alarm As String = AlarmsGrid.Rows(e.NewEditIndex).Cells(5).Text
            Dim auto_acknowledge As String = AlarmsGrid.Rows(e.NewEditIndex).Cells(6).Text

           
        End If
        AlarmsGrid.DataSource = AlarmModification
        AlarmsGrid.DataBind()
        FilterDataBind()
    End Sub




    Protected Sub AlarmGrid_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)

        'Dim AlarmID As Integer = Int.Parse(AlarmGrid.Rows(e.RowIndex).Cells(0)).Text
        Dim description As String = (AlarmGrid.Rows(e.RowIndex).Cells(2)).Text
        Dim mode As Integer = CInt(Val((AlarmGrid.Rows(e.RowIndex).Cells(3)).Text))
        Dim active As String = (AlarmGrid.Rows(e.RowIndex).Cells(4)).Text
        Dim regulatory_alarm As String = (AlarmGrid.Rows(e.RowIndex).Cells(5)).Text
        Dim auto_acknowledge As String = (AlarmGrid.Rows(e.RowIndex).Cells(6)).Text
        Dim rowsAffected As Integer
        Dim AlarmConn As SqlConnection
        AlarmConn = New SqlConnection(ConfigurationManager.ConnectionStrings("CMSConnStr").ConnectionString)
        Dim QueryString As String = String.Empty
        Dim PlantCommand As SqlCommand = New SqlCommand()
        Try
            AlarmConn.Open()
            QueryString = "Update Alarm_Definition " & _
                 " set description = @description,  active = @active , regulatory_alarm = @regulatory_alarm , " & _
                 "auto_acknowledge = @auto_acknowledge  where(Alarm_id = " & Me.AlarmId & " and Unit_id = " & Me.UnitId & " )"

            PlantCommand = New SqlCommand(QueryString, AlarmConn)

            PlantCommand.Parameters.Add("@description", SqlDbType.VarChar, 100).Value = description
            PlantCommand.Parameters.Add("@mode", SqlDbType.Int, 1).Value = mode
            PlantCommand.Parameters.Add("@active", SqlDbType.Char, 1).Value = active
            PlantCommand.Parameters.Add("@regulatory_alarm", SqlDbType.Char, 1).Value = regulatory_alarm
            PlantCommand.Parameters.Add("@auto_acknowledge", SqlDbType.Char, 1).Value = auto_acknowledge
            rowsAffected = PlantCommand.ExecuteNonQuery()
            'End If
        Catch ex As Exception
            Me.AlarmErrorMsg = ex.Message
            Dim errorMsg As String = ex.Message & " :Exception Inside AlarmModification.AlarmGrid_RowUpdating( )"
            'Check if an exception occured while logging to the database
            'Initially store the errorMsg in the CLogger property
            CLogger.LogDB = "N"
            CLogger.CustomErrorMsg = errorMsg
            If CLogger.WriteLogMsg(errorMsg, "E") Then
                CLogger.LogDB = "Y"
            End If
            Throw New Exception(errorMsg)
        End Try
        'EditAlarmRecord(UnitId, active, description, RegulatoryAlarm, AutoAcknowledge)

        AlarmGrid.EditIndex = -1

        AlarmGrid.DataSource = AlarmModification
        AlarmGrid.DataBind()
        FilterDataBind()

    End Sub

 
I appreciate your help. Please respond ASAP .
Thanks,

Sreekanth
0
 
DjDezmondCommented:
Did you try "AlarmsGrid.SelectedRows(0)" instead of 'e.NewEditIndex'?

You can query a cells contents like this:
  AlarmsGrid.SelectedRows(0).Cells(2).value
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now