Link to home
Start Free TrialLog in
Avatar of Member_2_6479049
Member_2_6479049

asked on

Index was out of range. Must be non-negative and less than the size of the collection parameter name:index

Hello guys, I'm getting this error but I find the solution, the error occurs in line 11. I hope you can help me please.


    Private Sub cmdVehActualizar_Click(sender As Object, e As EventArgs) Handles cmdVehActualizar.Click
        If Trim(cgClavePersona) = "" Then
            MsgBox("DEBE ESCRIBIR LOS DATOS OBLIGATORIOS ANTES DE USAR ESTE BOTON")
        End If

        If Trim(txtVehMarca.Text) = "" Or Trim(txtVehModelo.Text) = "" Or Trim(txtVehAno.Text) = "" Or Trim(txtVehPlacas.Text) = "" Then
            MsgBox("CAPTURE LOS MARCA, MODELO, AÑO Y PLACAS")
            Exit Sub
        End If

        Dim idUpdateAutos As String = dgVehiculos.SelectedRows(0).Cells(0).Value

        UpdateAutos(idUpdateAutos)

    End Sub

    Sub UpdateAutos(idUpdateAutos As String)

        Dim cVehFecVencePlacas As String = Trim(txtVehAnoVencimientoPlacas.Text) & "/" & Trim(txtVehMesVencimientoPlacas.Text) & "/" & Trim(txtVehDiaVencimientoPlacas.Text)
        Dim cVehIniCoberPoliza As String = Trim(txtVehAnoInicialPolizaSeguro.Text) & "/" & Trim(txtVehMesInicialPolizaSeguro.Text) & "/" & Trim(txtVehDiaInicialPolizaSeguro.Text)
        Dim cVehFinCoberPoliza As String = Trim(txtVehAnoFinalPolizaSeguro.Text) & "/" & Trim(txtVehMesFinalPolizaSeguro.Text) & "/" & Trim(txtVehDiaFinalPolizaSeguro.Text)

        Dim a1, b1, c1, d1, e1, f1, g1, h1, i1, j1, k1, l1, m1, n1, o1 As String

        a1 = cgClavePersona
        b1 = Trim(txtVehMarca.Text)
        c1 = Trim(txtVehModelo.Text)
        d1 = Trim(txtVehAno.Text)
        e1 = Trim(txtVehPlacas.Text)
        f1 = Trim(txtVehColor.Text)
        g1 = Trim(txtVehCalcomania.Text)
        h1 = Trim(txtVehSerie.Text)
        i1 = cVehFecVencePlacas
        j1 = Trim(txtVehKilometraje.Text)
        k1 = Trim(txtVehKilometrajeBaja.Text)
        l1 = Trim(txtAseguradora.Text)
        m1 = Trim(txtVehNumeroPolizaSeguro.Text)
        n1 = cVehIniCoberPoliza
        o1 = cVehFinCoberPoliza

        Dim cSQLActV As String = ("UPDATE vehiculos SET Clave_Persona='" + a1 + "', Marca='" + b1 + "', Modelo='" + c1 + "', Ano='" + d1 + "', Placas='" + e1 + "', Color='" + f1 + "'," & _
                                 "Num_Calcomania='" + g1 + "', Serie='" + h1 + "', Fec_Vence_Placas='" + i1 + "', Kilometraje_Registrar='" + j1 + "', Kilometraje_Baja='" + k1 + "', Aseguradora='" + l1 + "'," & _
                                 "Num_Pol_Seguro='" + m1 + "', Fec_Ini_Pol_Seguro='" + n1 + "', Fec_Fin_Pol_Seguro='" + o1 + "' WHERE vehiculos.clave = '" + idUpdateAutos + "'")

        Try
            If Conn.State = ConnectionState.Closed Then
                Conn.Open()
            End If

            Adapter.UpdateCommand = Conn.CreateCommand()
            Adapter.UpdateCommand.CommandText = cSQLActV

            If Adapter.UpdateCommand.ExecuteNonQuery() > 0 Then
                MsgBox("EL REGISTRO SE ACTUALIZO CORRECTAMENTE!")
                DespliegaInformacionVehicular()
                LimpiaFormaVehiculos()
            End If
            Conn.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
            Conn.Close()
        End Try

Open in new window

Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Hi,

Can you debug your code and provide us the code where it is failing?
Avatar of Member_2_6479049
Member_2_6479049

ASKER

The problem is in line 3

    Private Sub cmdVehActualizar_Click(sender As Object, e As EventArgs) Handles cmdVehActualizar.Click

        Dim idUpdateAutos As String = dgVehiculos.SelectedRows(0).Cells(0).Value

        UpdateAutos(idUpdateAutos)

    End Sub

Open in new window

Could you please provide us code for UpdateAutos(idUpdateAutos)? I mean what is written in that function.
Sure, thankyou Pawan

 Sub UpdateAutos(idUpdateAutos As String)

        Dim cVehFecVencePlacas As String = Trim(txtVehAnoVencimientoPlacas.Text) & "/" & Trim(txtVehMesVencimientoPlacas.Text) & "/" & Trim(txtVehDiaVencimientoPlacas.Text)
        Dim cVehIniCoberPoliza As String = Trim(txtVehAnoInicialPolizaSeguro.Text) & "/" & Trim(txtVehMesInicialPolizaSeguro.Text) & "/" & Trim(txtVehDiaInicialPolizaSeguro.Text)
        Dim cVehFinCoberPoliza As String = Trim(txtVehAnoFinalPolizaSeguro.Text) & "/" & Trim(txtVehMesFinalPolizaSeguro.Text) & "/" & Trim(txtVehDiaFinalPolizaSeguro.Text)

        Dim a1, b1, c1, d1, e1, f1, g1, h1, i1, j1, k1, l1, m1, n1, o1 As String

        a1 = cgClavePersona
        b1 = Trim(txtVehMarca.Text)
        c1 = Trim(txtVehModelo.Text)
        d1 = Trim(txtVehAno.Text)
        e1 = Trim(txtVehPlacas.Text)
        f1 = Trim(txtVehColor.Text)
        g1 = Trim(txtVehCalcomania.Text)
        h1 = Trim(txtVehSerie.Text)
        i1 = cVehFecVencePlacas
        j1 = Trim(txtVehKilometraje.Text)
        k1 = Trim(txtVehKilometrajeBaja.Text)
        l1 = Trim(txtAseguradora.Text)
        m1 = Trim(txtVehNumeroPolizaSeguro.Text)
        n1 = cVehIniCoberPoliza
        o1 = cVehFinCoberPoliza

        Dim cSQLActV As String = ("UPDATE vehiculos SET Clave_Persona='" + a1 + "', Marca='" + b1 + "', Modelo='" + c1 + "', Ano='" + d1 + "', Placas='" + e1 + "', Color='" + f1 + "'," & _
                                 "Num_Calcomania='" + g1 + "', Serie='" + h1 + "', Fec_Vence_Placas='" + i1 + "', Kilometraje_Registrar='" + j1 + "', Kilometraje_Baja='" + k1 + "', Aseguradora='" + l1 + "'," & _
                                 "Num_Pol_Seguro='" + m1 + "', Fec_Ini_Pol_Seguro='" + n1 + "', Fec_Fin_Pol_Seguro='" + o1 + "' WHERE vehiculos.clave = '" + idUpdateAutos + "'")

        Try
            If Conn.State = ConnectionState.Closed Then
                Conn.Open()
            End If

            Adapter.UpdateCommand = Conn.CreateCommand()
            Adapter.UpdateCommand.CommandText = cSQLActV

            If Adapter.UpdateCommand.ExecuteNonQuery() > 0 Then
                MsgBox("EL REGISTRO SE ACTUALIZO CORRECTAMENTE!")
                DespliegaInformacionVehicular()
                LimpiaFormaVehiculos()
            End If
            Conn.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
            Conn.Close()
        End Try

Open in new window

Are you getting error in below-?

DespliegaInformacionVehicular()
LimpiaFormaVehiculos()
Actually, DespliegaInformacionVehicular() and LimpiaFormaVehiculos() works fine, The form contains text boxes and the DataGridView and when I add the values from text boxes to the DataGridView , the values shows fine and are stored on the data base.

So at this point everything works fine, then when I try to update I select the row from  DataGridView and the the text boxes are filled fine. When I click on the Update Botton appears the error in this line:

Dim idUpdateAutos As String = dgVehiculos.SelectedRows(0).Cells(0).Value

This is the routine I'm using to fill from the DataGridView to text boxes
    Private Sub dgVehiculos_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgVehiculos.CellClick
        Dim cvCveReg As String = dgVehiculos.Rows(e.RowIndex).Cells(0).Value.ToString()
        Dim cvCvePersona As String = dgVehiculos.Rows(e.RowIndex).Cells(1).Value.ToString()
        Dim cvMarca As String = dgVehiculos.Rows(e.RowIndex).Cells(2).Value.ToString()
        Dim cvModelo As String = dgVehiculos.Rows(e.RowIndex).Cells(3).Value.ToString()
        Dim cvAno As String = dgVehiculos.Rows(e.RowIndex).Cells(4).Value.ToString()
        Dim cvPlacas As String = dgVehiculos.Rows(e.RowIndex).Cells(5).Value.ToString()
        Dim cvColor As String = dgVehiculos.Rows(e.RowIndex).Cells(6).Value.ToString()
        Dim cvNumCalcoma As String = dgVehiculos.Rows(e.RowIndex).Cells(7).Value.ToString()
        Dim cvSerie As String = dgVehiculos.Rows(e.RowIndex).Cells(8).Value.ToString()
        Dim cvFecVencePlacas As String = dgVehiculos.Rows(e.RowIndex).Cells(9).Value.ToString()
        Dim cvKiloIni As String = dgVehiculos.Rows(e.RowIndex).Cells(10).Value.ToString()
        Dim cvKiloFin As String = dgVehiculos.Rows(e.RowIndex).Cells(11).Value.ToString()
        Dim cvAsegura As String = dgVehiculos.Rows(e.RowIndex).Cells(12).Value.ToString()
        Dim cvNumPolAsegura As String = dgVehiculos.Rows(e.RowIndex).Cells(13).Value.ToString()
        Dim cvFecIniCober As String = dgVehiculos.Rows(e.RowIndex).Cells(14).Value.ToString()
        Dim cvFecFinCober As String = dgVehiculos.Rows(e.RowIndex).Cells(15).Value.ToString()

        Select Case Mid(cvFecVencePlacas, 1, 2)
            Case Is <> "00"
                txtVehDiaVencimientoPlacas.Text = Mid(cvFecVencePlacas, 1, 2)
                txtVehMesVencimientoPlacas.Text = Mid(cvFecVencePlacas, 4, 2)
                txtVehAnoVencimientoPlacas.Text = Mid(cvFecVencePlacas, 7, 4)
            Case Else
                txtVehDiaVencimientoPlacas.Text = ""
                txtVehMesVencimientoPlacas.Text = ""
                txtVehAnoVencimientoPlacas.Text = ""
        End Select

        Select Case Mid(cvFecIniCober, 1, 2)
            Case Is <> "00"
                txtVehDiaInicialPolizaSeguro.Text = Mid(cvFecIniCober, 1, 2)
                txtVehMesInicialPolizaSeguro.Text = Mid(cvFecIniCober, 4, 2)
                txtVehAnoInicialPolizaSeguro.Text = Mid(cvFecIniCober, 7, 4)
            Case Else
                txtVehDiaInicialPolizaSeguro.Text = ""
                txtVehMesInicialPolizaSeguro.Text = ""
                txtVehAnoInicialPolizaSeguro.Text = ""
        End Select

        Select Case Mid(cvFecFinCober, 1, 2)
            Case Is <> "00"
                txtVehDiaFinalPolizaSeguro.Text = Mid(cvFecFinCober, 1, 2)
                txtVehMesFinalPolizaSeguro.Text = Mid(cvFecFinCober, 4, 2)
                txtVehAnoFinalPolizaSeguro.Text = Mid(cvFecFinCober, 7, 4)
            Case Else
                txtVehDiaFinalPolizaSeguro.Text = ""
                txtVehMesFinalPolizaSeguro.Text = ""
                txtVehAnoFinalPolizaSeguro.Text = ""
        End Select

        txtVehMarca.Text = cvMarca
        txtVehModelo.Text = cvModelo
        txtVehAno.Text = cvAno
        txtVehPlacas.Text = cvPlacas
        txtVehColor.Text = cvColor
        txtVehCalcomania.Text = cvNumCalcoma
        txtVehSerie.Text = cvSerie
        txtVehKilometraje.Text = cvKiloIni
        txtVehKilometrajeBaja.Text = cvKiloFin
        txtAseguradora.Text = cvAsegura
        txtVehNumeroPolizaSeguro.Text = cvNumPolAsegura

    End Sub

Open in new window

SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
De and Fer, Thank you so much for you help, it is already solved.

I use the index as you guys suggested

Thank you!!!
Thank you guys!