?
Solved

Index was out of range

Posted on 2006-11-05
8
Medium Priority
?
359 Views
Last Modified: 2012-05-05
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
Comment
Question by:Sreekanth_Nagabandi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 2000 total points
ID: 17878111
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
 

Author Comment

by:Sreekanth_Nagabandi
ID: 17878711
Hi,

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

Thanks,
Sreekanth
0
 
LVL 4

Expert Comment

by:boy8964
ID: 17879030
make sure e.NewEditIndex is not less then 0.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Sreekanth_Nagabandi
ID: 17879038
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
 
LVL 9

Expert Comment

by:DjDezmond
ID: 17879875
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
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17880346
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
 

Author Comment

by:Sreekanth_Nagabandi
ID: 17881185
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
 
LVL 9

Expert Comment

by:DjDezmond
ID: 17881680
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question