Foros del Web » Programación para mayores de 30 ;) » .NET »

El índice estaba fuera del intervalo.

Estas en el tema de El índice estaba fuera del intervalo. en el foro de .NET en Foros del Web. Tengo este error en mi programa que estoy haciendo, tengo otros 4 formularios iguales pero no se porque en este me esta saliendo el error: ...
  #1 (permalink)  
Antiguo 17/11/2014, 13:57
 
Fecha de Ingreso: noviembre-2014
Mensajes: 1
Antigüedad: 9 años, 5 meses
Puntos: 0
El índice estaba fuera del intervalo.

Tengo este error en mi programa que estoy haciendo, tengo otros 4 formularios iguales pero no se porque en este me esta saliendo el error:
El índice estaba fuera del intervalo. Debe ser un valor no negativo e inferior al tamaño de la colección. Nombre del parametro: index

Aca les dejo la parte de mi formulario

__________________________________________________ _________________

Public Class FromProductos
Private dt As New DataTable
Private Sub FromProductos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mostrar()
End Sub
Public Sub limpiar()
btnguardar.Visible = True
btnEditar.Visible = False
txtID.Text = ""
txtmarca.Text = ""
txtnomMarca.Text = ""
txtgeneracion.Text = ""
txtTipo.Text = ""
txtprecio.Text = ""
End Sub

Private Sub mostrar()
Try
Dim func As New fproducto
dt = func.mostrar
DataListado.Columns.Item("Eliminar").Visible = False


If dt.Rows.Count <> 0 Then
DataListado.DataSource = dt
txtBuscar.Enabled = True
DataListado.ColumnHeadersVisible = True
inexistente.Visible = False
Else
DataListado.DataSource = Nothing
txtBuscar.Enabled = False
DataListado.ColumnHeadersVisible = False
inexistente.Visible = True
End If
btnAceptar.Visible = True
btnEditar.Visible = False
buscar()


Catch ex As Exception
MsgBox(ex.Message)

End Try

End Sub
Private Sub buscar()
Try
Dim ds As New DataSet
ds.Tables.Add(dt.Copy)
Dim dv As New DataView(ds.Tables(0))


dv.RowFilter = cboCampo.Text & " like '" & txtBuscar.Text & "%'"

If dv.Count <> 0 Then
inexistente.Visible = False
DataListado.DataSource = dv
ocultar_columnas()

Else
inexistente.Visible = True
DataListado.DataSource = Nothing
End If

Catch ex As Exception
MsgBox(ex.Message)

End Try
End Sub
Private Sub ocultar_columnas()

End Sub

Private Sub btnguardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnguardar.Click
Try
If Me.ValidateChildren = True And txtID.Text <> "" And txtmarca.Text <> "" And txtnomMarca.Text <> "" And txtgeneracion.Text <> "" And txtprecio.Text <> "" And txtTipo.Text <> "" Then

Dim dts As New vproducto
Dim func As New fproducto

dts.gidproducto = txtID.Text
dts.gidmarca = txtmarca.Text
dts.ggeneracion = txtgeneracion.Text
dts.gprecio = txtprecio.Text
dts.gidalmacen = txtTipo.Text

If func.insertar(dts) Then
MessageBox.Show("Producto registrado correctamente", "Guardando datos", MessageBoxButtons.OK, MessageBoxIcon.Information)
mostrar()
limpiar()
cargar_detalle()
Else
MessageBox.Show("Producto no registrado correctamente", "Guardando datos", MessageBoxButtons.OK, MessageBoxIcon.Error)
mostrar()

limpiar()
End If
Else
MessageBox.Show("Falta ingresar algunos datos", "Guardando datos", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub DataListado_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataListado.CellClick
Try
txtID.Text = DataListado.SelectedCells.Item(1).Value
txtmarca.Text = DataListado.SelectedCells.Item(2).Value
txtnomMarca.Text = DataListado.SelectedCells.Item(3).Value
txtTipo.Text = DataListado.SelectedCells.Item(4).Value
txtgeneracion.Text = DataListado.SelectedCells.Item(5).Value
txtprecio.Text = DataListado.SelectedCells.Item(6).Value
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub DataListado_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataListado.CellContentClick
Try
If e.ColumnIndex = Me.DataListado.Columns.Item("Eliminar").Index Then
Dim chkcell As DataGridViewCheckBoxCell = Me.DataListado.Rows(e.RowIndex).Cells("Eliminar")
chkcell.Value = Not chkcell.Value
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
Try
Dim result As DialogResult

result = MessageBox.Show("Realmente desea editar los datos de la venta?", "MOdificando registros", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)

If result = DialogResult.OK Then

If Me.ValidateChildren = True And txtID.Text <> "" And txtmarca.Text <> "" And txtnomMarca.Text <> "" And txtgeneracion.Text <> "" And txtprecio.Text <> "" And txtTipo.Text <> "" Then
Try
Dim dts As New vproducto
Dim func As New fproducto

dts.gidproducto = txtID.Text
dts.gidmarca = txtmarca.Text
dts.ggeneracion = txtgeneracion.Text
dts.gprecio = txtprecio.Text
dts.gidalmacen = txtprecio.Text

If func.editar(dts) Then
MessageBox.Show("Producto modficado correctamente", "Modificado registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
mostrar()
limpiar()
Else
MessageBox.Show("Producto no fue modifcada intente de nuevo", "Modificando registros", MessageBoxButtons.OK, MessageBoxIcon.Error)
mostrar()
limpiar()
End If

Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MessageBox.Show("Falta ingresar algunos datos", "Modificando registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub cbeliminar_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbEliminar.CheckedChanged
Try
If cbEliminar.CheckState = CheckState.Checked Then
DataListado.Columns.Item("Eliminar").Visible = True
Else
DataListado.Columns.Item("Eliminar").Visible = False
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Public Sub cargar_detalle()
Try


FormAgregarMarca.txtID.Text = DataListado.SelectedCells.Item(1).Value
FormAgregarMarca.txtmarca.Text = DataListado.SelectedCells.Item(2).Value
FormAgregarMarca.txtnomMarca.Text = DataListado.SelectedCells.Item(3).Value
FormAgregarMarca.txtgeneracion.Text = DataListado.SelectedCells.Item(4).Value
FormAgregarMarca.txtTipo.Text = DataListado.SelectedCells.Item(5).Value
FormAgregarMarca.txtTipo.Text = DataListado.SelectedCells.Item(6).Value

FormAgregarMarca.ShowDialog()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub txtbuscar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.TextChanged
buscar()
End Sub


Private Sub btnAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
limpiar()
mostrar()

End Sub
Private Sub btnAlmacen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAlmacen.Click
FormAlmacen.txtflag.Text = "1"
FormAlmacen.ShowDialog()
End Sub
Private Sub btnMarca_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMarca.Click
FormMarca.txtflag.Text = "1"
FormMarca.ShowDialog()
End Sub

End Class
  #2 (permalink)  
Antiguo 18/11/2014, 08:12
Avatar de lucasmoadr  
Fecha de Ingreso: marzo-2007
Ubicación: Llavallol, Buenos Aires
Mensajes: 220
Antigüedad: 17 años, 1 mes
Puntos: 5
Respuesta: El índice estaba fuera del intervalo.

te aconsejo que debuguees y vas a ver bien en donde te esta fallando. y ahi si puedo darte una mano. Asi con tanto codigo es complicado darse cuenta. No indicaste si te falla cuando carga el formulario, cuando seleccionas algo, etc.

Saludos!
__________________
Alquileres en la costa
Se tu mismo
  #3 (permalink)  
Antiguo 18/11/2014, 15:34
Avatar de Drako_18  
Fecha de Ingreso: mayo-2005
Ubicación: Madrid
Mensajes: 505
Antigüedad: 19 años
Puntos: 16
Respuesta: El índice estaba fuera del intervalo.

Buenas compañero,

Por un rápido vistazo... Haces la llamada a una tabla(0) pero seguramente ese DataSet no tenga dicha tabla.. (98% de posibilidad)


Puede ser que el índice de...
FormAgregarMarca.txtID.Text = DataListado.SelectedCells.Item(1).Value
FormAgregarMarca.txtmarca.Text = DataListado.SelectedCells.Item(2).Value
FormAgregarMarca.txtnomMarca.Text = DataListado.SelectedCells.Item(3).Value
FormAgregarMarca.txtgeneracion.Text = DataListado.SelectedCells.Item(4).Value
FormAgregarMarca.txtTipo.Text = DataListado.SelectedCells.Item(5).Value
FormAgregarMarca.txtTipo.Text = DataListado.SelectedCells.Item(6).Value

Debiera comenzar por 0 y no por 1 y al llegar al 6, debería ser el 5... Y peta.


Y tercero y más importante...
Utiliza el F10 para debuguear, que esto que has hecho es totalmente de vagos... Decir "Me dice que está fuera del rango, tomad este es mi código..." y quedarse tan a gusto...
Tienes 20 millones de "MsgBox(ex.Message)" en los Try Catch... Dales uso, que para eso se ponen.
Ya no es cuestión de ser novato o no, de haber publicado más veces o no... Sino de sentido común. Ni un saludo, ni un gracias ni nada y ale, churro de código y que otro lo depure al hacer dios sabe qué.


Un saludo!
__________________
Rubén Espada
Desarrollador full stack .Net (Angular + JS + .Net Core)

Etiquetas: visual
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:28.