Solved

Index was out of range

Posted on 2006-11-05
8
350 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 500 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now