Este es el diseño que tengo como ejemplo:
https://imageshack.com/a/img922/9868/XlQkSg.jpg
Código:
Imports System.Data
Imports System.Data.OleDb
Public Class Form2
Dim conexion As New OleDbConnection
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
conexion.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\jose\Documents\Visual Studio 2005\Projects\RJ Sports\RJ Sports\bin\Registro.mdb"
MsgBox(" Conectado Correctamente ", vbInformation, " Conectado ")
Catch ex As Exception
MsgBox(" Error al conectar con la base de datos ", vbCritical, " Error ")
End Try
End Sub
Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
conexion.Open()
Try
Dim cmd As OleDbCommand
Dim r As Integer
cmd = New OleDbCommand("insert into Registro(Cedula, Nombre, Apellido, Edad)values (' " & TextBox1.Text & " ', ' " & TextBox2.Text & " ', ' " & TextBox3.Text & " ', ' " & TextBox4.Text & " '),conexion")
r = cmd.ExecuteNonQuery
If r > 0 Then
MsgBox(" Datos guardados correctamente ", vbInformation, " Guardado ")
End If
Catch ex As Exception
MsgBox(" No se ha podido guardar los datos ", vbCritical, " Error ")
End Try
conexion.Close()
End Sub
Private Sub btnSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalir.Click
Dim x As String
x = MsgBox(" Desea Salir? ", vbQuestion + vbYesNo)
If x = vbYes Then
Close()
End If
End Sub
Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
conexion.Open()
Try
Dim adaptador As OleDbDataAdapter
Dim tabla As DataTable
Dim buscar As String
If ComboBox1.Text = " Todos " Then
buscar = ("select * from Registro")
adaptador = New OleDbDataAdapter(buscar, conexion)
tabla = New DataTable
adaptador.Fill(tabla)
DataGridView1.DataSource = tabla
Else
buscar = ("select * from Registro where nombre = '" & ComboBox1.Text & " ' ")
adaptador = New OleDbDataAdapter(buscar, conexion)
tabla = New DataTable
adaptador.Fill(tabla)
DataGridView1.DataSource = tabla
End If
Catch ex As Exception
MsgBox(ex.Message, vbCritical, " Error ")
End Try
conexion.Close()
End Sub
Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
conexion.Open()
Try
Dim cmd1 As OleDbCommand
Dim resul As MsgBoxResult
If ComboBox1.Text = "Todos" Or ComboBox1.Text = "todos" Then
resul = MsgBox(" Estas seguro de querer eliminar todos los registros de la tabla? ", vbQuestion + vbYesNo, " Advertencia ")
If resul = vbYes Then
cmd1 = New OleDbCommand("delete from Registro", conexion)
cmd1.ExecuteNonQuery()
MsgBox(" Todos los datos se eliminaron correctamente ", vbExclamation, " Correcto ")
End If
Else
cmd1 = New OleDbCommand("delete from Registro where nombre = '" & ComboBox1.Text & " ' ", conexion)
cmd1.ExecuteNonQuery()
MsgBox(" Registro eliminado correctamente ", vbInformation, " Correcto ")
End If
Catch ex As Exception
MsgBox(ex.Message, vbCritical, " Error ")
End Try
conexion.Close()
End Sub
Private Sub btnActualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnActualizar.Click
Try
My.Forms.Form3.Actualizar_Cedula.Text = DataGridView1.SelectedRows.Item(1).Cells(1).Value.ToString
My.Forms.Form3.Actualizar_Nombre.Text = DataGridView1.SelectedRows.Item(2).Cells(2).Value.ToString
My.Forms.Form3.Actualizar_Apellido.Text = DataGridView1.SelectedRows.Item(3).Cells(3).Value.ToString
My.Forms.Form3.Actualizar_Edad.Text = DataGridView1.SelectedRows.Item(4).Cells(4).Value.ToString
My.Forms.Form3.Actualizar_Cedula.Enabled = False
Form3.Show()
Catch ex As Exception
MsgBox(" Error: " & ex.Message.ToString)
End Try
End Sub
End Class
Cuando quiero guardar un registro me sale el mensaje de "No se ha podido guardar los datos"
El error al dar click en Actualizar dice esto: "Error: el indice estaba fuera del intervalo. Debe ser un valor no negativo e inferior al tamaño de la colección.
Nombre del parametro: index"
No tengo mas idea de que pueda estar generando esos problemas. Saludos