Solved

Index was out of range

Posted on 2006-11-05
8
356 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 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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