Ver Mensaje Individual
  #1 (permalink)  
Antiguo 05/10/2008, 09:00
arcr
 
Fecha de Ingreso: julio-2008
Mensajes: 52
Antigüedad: 15 años, 9 meses
Puntos: 0
Error: Update requiere que insertcommand sea valido cuando se pasa la coleccion data

HOLA AMIGOS:

QUIEN SERIA TAN AMABLE DE AYUDARME CON ESTE ERROR?

QUIERO AÑADIR 3 CAMPOS EN UNA TABLA HECHA EN UNA BASE DE DATOS EN ACCESS, CREO UN OLEDBDATAADAPTER CON CODIGO Y UN DATASET PERO NO SE SI LO ESTOY HACIENDO DE LA MANERA CORRECTA PORQUE NO ME PERMITE GRABAR LOS DATOS EN LA RESPECTIVA TABLA

EL ERROR QUE ME PRESENTA ES EL SIGUIENTE:

UPDATE REQUIERE QUE INSERTCOMMAND SEA VALIDO CUANDO SE PASA LA COLECCION DATA ROW CON NUEVAS FILAS

Y EL CODIGO QUE TENGO ES EL SIGUIENTE:

Private Sub cmdgrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdgrabar.Click

'Se define una conexion común
Dim conexion As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=c:\Practicas.NET\Base de Datos\Control_Escolar.mdb")

Dim Adaptador As New OleDbDataAdapter()
'Abrir la conexion
conexion.Open()
'Creamos un comando asociado con la conexion
Dim comando As OleDbCommand = conexion.CreateCommand()
'Indicando el tipo y el nombre de la tabla
comando.CommandType = CommandType.TableDirect
comando.CommandText = "Alumnos" 'Aquí cambias el nombre de la tabla que se desea
'Asociamos el comando con el adaptador
Adaptador.SelectCommand = comando
'Creamos el dataset
Dim Datos As New DataSet("Alumnos") 'ojo también puede ir sin tabla
'Llenamos de datos el adaptador
Adaptador.Fill(Datos, "Alumnos")
Dim Tabla_Alumnos As DataTable = Datos.Tables("Alumnos")
Dim Registros_Alumnos As DataRowCollection = Tabla_Alumnos.Rows
'Muestro los registros de la tabla

Dim j, n As Double
Dim gdo As Integer
Dim gpo, Tabla As String

Dim conexion2 As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=c:\Practicas.NET\Sistema_Primaria\Ciclo2008 2009.mdb")
Dim Adaptador2 As New OleDbDataAdapter()
conexion2.Open()
Dim comando2 As OleDbCommand = conexion2.CreateCommand()
comando2.CommandType = CommandType.TableDirect
'Creamos el dataset
Dim Datos2 As New DataSet() 'ojo también puede ir sin tabla




For j = 0 To Registros_Alumnos.Count - 1
If Registros_Alumnos(j)(8) = "A" Then
gdo = Registros_Alumnos(j)(6)
gpo = Registros_Alumnos(j)(7)

If gdo >= 1 And gdo <= 2 Then
For n = 1 To 5

comando2.CommandText = Trim$("Lista" & gdo & Trim$(gpo) & "M0" & n)
Tabla = Trim$("Lista" & gdo & Trim$(gpo) & "M0" & n)
'Asociamos el comando con el adaptador
Adaptador2.SelectCommand = comando2
'Llenamos de datos el adaptador
Adaptador2.Fill(Datos2, Tabla)

Dim Tabla_Listas As DataTable = Datos2.Tables(Tabla)
Dim Registros_Listas As DataRowCollection = Tabla_Listas.Rows

'Añadir un registro nuevo
Dim nuevafila As DataRow

'Nueva fila
nuevafila = Tabla_Listas.NewRow()
nuevafila(0) = Registros_Alumnos(j)(0)
nuevafila(1) = Registros_Alumnos(j)(2)
nuevafila(2) = Registros_Alumnos(j)(3)
Registros_Listas.Add(nuevafila)

Try
Adaptador2.Update(Datos2.Tables(Tabla))
Catch ex As Exception
'Capturar cualquier posible error, por ejem. llave duplicada
MessageBox.Show(ex.Message)
End Try
Next n

ElseIf gdo = 3 Then
For n = 1 To 6
comando2.CommandText = Trim$("Lista" & gdo & Trim$(gpo) & "M0" & n)
Tabla = Trim$("Lista" & gdo & Trim$(gpo) & "M0" & n)
'Asociamos el comando con el adaptador
Adaptador2.SelectCommand = comando2
'Llenamos de datos el adaptador
Adaptador2.Fill(Datos2, Tabla)

Dim Tabla_Listas As DataTable = Datos2.Tables(Tabla)
Dim Registros_Listas As DataRowCollection = Tabla_Listas.Rows

'Añadir un registro nuevo
Dim nuevafila As DataRow

'Nueva fila
nuevafila = Tabla_Listas.NewRow()
nuevafila(0) = Registros_Alumnos(j)(0)
nuevafila(1) = Registros_Alumnos(j)(2)
nuevafila(2) = Registros_Alumnos(j)(3)
Registros_Listas.Add(nuevafila)

Try
Adaptador2.Update(Datos2.Tables(Tabla))
Catch ex As Exception
'Capturar cualquier posible error, por ejem. llave duplicada
MessageBox.Show(ex.Message)
End Try
Next n

ElseIf gdo >= 4 And gdo <= 6 Then
For n = 1 To 8
comando2.CommandText = Trim$("Lista" & gdo & Trim$(gpo) & "M0" & n)
Tabla = Trim$("Lista" & gdo & Trim$(gpo) & "M0" & n)
'Asociamos el comando con el adaptador
Adaptador2.SelectCommand = comando2
'Llenamos de datos el adaptador
Adaptador2.Fill(Datos2, Tabla)

Dim Tabla_Listas As DataTable = Datos2.Tables(Tabla)
Dim Registros_Listas As DataRowCollection = Tabla_Listas.Rows

'Añadir un registro nuevo
Dim nuevafila As DataRow

'Nueva fila
nuevafila = Tabla_Listas.NewRow()
nuevafila(0) = Registros_Alumnos(j)(0)
nuevafila(1) = Registros_Alumnos(j)(2)
nuevafila(2) = Registros_Alumnos(j)(3)
Registros_Listas.Add(nuevafila)

Try
Adaptador2.Update(Datos2.Tables(Tabla))
Catch ex As Exception
'Capturar cualquier posible error, por ejem. llave duplicada
MessageBox.Show(ex.Message)
End Try
Next n
End If

End If
Next j


'Cerramos la conexion
conexion.Close()
conexion2.Close()

End Sub


A GRANDES RASGOS LES EXPLICO LO QUE HACE EL BOTON GRABAR
TENGO QUE FILTRAR LOS ALUMNOS QUE TENGO EN LA TABLA Alumnos DE LA BASE DE DATOS QUE ESTA EN LA CONTROL_ESCOLAR.MDB POR ESO NECESITO UN CICLO FOR J=0 TO REGISTROS_ALUMNOS.COUNT-1 PARA QUE VAYA DE UNO POR UNO Y SE GUARDA SU GRADO gdo Y SU GRUPO gpo EN SUS RESPECTIVAS VARIABLES PARA ESCOGER SU LISTA CORRESPONDIENTE DEPENDIENDO DEL GRADO Y EL GRUPO EN QUE SE ENCUENTREN Y DEPENDE DEL GRADO SE HACE OTRO CICLO PORQUE ES UNA LISTA POR MATERIA Y DEPENDIENDO DEL GRADO SE TIENEN MAS O MENOS MATERIAS.

BUENO ESTO ES PARA QUE ENTIENDAN UN POCO MAS MI CODIGO Y MI OBJETIVO A REALIZAR PERO NO HAN SIDO POSIBLE GRABAR LOS DATOS EN LAS LISTAS POR EL ERROR

ESPERO QUE ALGUIEN PUEDA AYUDARME

DE ANTEMANO MUCHAS GRACIAS Y BUENA SUERTE