Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Index was out of range

Posted on 2006-11-05
8
Medium Priority
?
362 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

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…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

824 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