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

Error en updatecommand

Estas en el tema de Error en updatecommand en el foro de .NET en Foros del Web. HOLA BUENO ENCONTRE UN EJEMPLO DE COMO CARGAR UNA IMAGEN A UNA BASE DE DATOS (SQL SERVER ) PERO EL CODIGO LANZA UN ERROR QUE ...
  #1 (permalink)  
Antiguo 27/01/2009, 15:11
Avatar de wladtepes  
Fecha de Ingreso: febrero-2008
Mensajes: 140
Antigüedad: 16 años, 3 meses
Puntos: 0
Error en updatecommand

HOLA BUENO ENCONTRE UN EJEMPLO DE COMO CARGAR UNA IMAGEN A UNA BASE DE DATOS (SQL SERVER ) PERO EL CODIGO LANZA UN ERROR QUE NO ENTIENDO


EL ERROR ES UPDATECOMMAND NO ES COMPATIBLE CON SELECTCOMMAND



Código PHP:

Imports System
.Data
Imports System
.Data.SqlClient
Imports System
.Drawing



Public Class Form1

    
'Private cnnStr As String = "Data Source=.\SQLEXPRESS; Initial Catalog=conImagenesSQL;Integrated Security=True;"
    Private cnnStr As String = "Integrated Security=true;Data Source=felipe\SQLEXPRESS;Initial Catalog=conImagenes2;"

    Private dt As New DataTable
    Private dtNavegador As TablaNavegar

    Private Sub habilitarControles(ByVal habilitar As Boolean)
        ' 
Habilitar/deshabilitar los controles según se indique
        Me
.BindingNavigatorDeleteItem.Enabled habilitar
        Me
.BindingNavigatorMoveFirstItem.Enabled habilitar
        Me
.BindingNavigatorMoveLastItem.Enabled habilitar
        Me
.BindingNavigatorMoveNextItem.Enabled habilitar
        Me
.BindingNavigatorMovePreviousItem.Enabled habilitar
    End Sub

    
Private Sub actualizarNavegacion()
        
' Actualizar los controles de navegación
        actualizarNavegacion(True)
    End Sub
    Private Sub actualizarNavegacion(ByVal mostrarFila As Boolean)
        ' 
Actualizar los controles de navegación
        
' Si mostrarFila = True se muestra la fila actual
        Me.IDTextBox.Text = ""
        Me.NombreTextBox.Text = ""
        Me.FotoPictureBox.Image = Nothing
        If mostrarFila AndAlso dtNavegador.NumeroUltimaFila >= 0 Then
            Dim dr As DataRow = dtNavegador.FilaActual
            If dr IsNot Nothing Then
                Me.IDTextBox.Text = dr("ID").ToString
                Me.NombreTextBox.Text = dr("Nombre").ToString
                If dr("Foto") IsNot DBNull.Value Then
                    Dim img As Image = TablaNavegar.Bytes2Image(CType(dr("Foto"), Byte()))
                    If img IsNot Nothing Then
                        Me.FotoPictureBox.Image = img
                        '
Else
                        
'    img = TryCast(dr("Foto"), Bitmap)
                        '    
If img IsNot Nothing Then
                        
'        Me.FotoPictureBox.Image = img
                        '    
End If
                    
End If
                
End If
            
End If
        
End If
        
habilitarControles(False)
        If 
dtNavegador.NumeroUltimaFila >= 0 Then
            BindingNavigatorPositionItem
.Text = (dtNavegador.NumeroFilaActual 1).ToString
            BindingNavigatorCountItem
.Text "de " & (dtNavegador.NumeroUltimaFila 1).ToString
            
'habilitarControles(True)
            If dtNavegador.NumeroFilaActual > 0 Then
                Me.BindingNavigatorMoveFirstItem.Enabled = True
                Me.BindingNavigatorMovePreviousItem.Enabled = True
            End If
            If dtNavegador.NumeroFilaActual < dtNavegador.NumeroUltimaFila Then
                Me.BindingNavigatorMoveLastItem.Enabled = True
                Me.BindingNavigatorMoveNextItem.Enabled = True
            End If
        Else
            BindingNavigatorPositionItem.Text = "0"
            BindingNavigatorCountItem.Text = "de 0"
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.IDTextBox.Enabled = False
        habilitarControles(False)
        Dim da As New SqlDataAdapter("SELECT * FROM conFotos", cnnStr)
        Try
            da.Fill(dt)
            dtNavegador = New TablaNavegar(dt)
            actualizarNavegacion(True)
        Catch ex As Exception
            MessageBox.Show("ERROR al cargar los datos:" & vbCrLf & _
                            ex.Message, "Error al cargar los datos", _
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            '
Exit Sub
        End 
Try
    
End Sub

    
Private Sub ConFotosBindingNavigatorSaveItem_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles ConFotosBindingNavigatorSaveItem.Click
        
' Guardar los datos en la tabla

*************************************************

[COLOR="Red"]LINEAS DE ERROR [/COLOR]


   Dim da As New SqlDataAdapter("SELECT * FROM conFotos", cnnStr)
        Try
            Dim cb As New SqlCommandBuilder(da)
            SqlCommandBuilder(cb) = New SqlCommandBuilder(da)
            da.SelectCommand= new SqlCommand(sql, cnnStr);







            da.UpdateCommand = cb.GetUpdateCommand(True)
            da.InsertCommand = cb.GetInsertCommand(True)
            da.DeleteCommand = cb.GetDeleteCommand(True)
            da.Update(dt)
            dt.AcceptChanges()

[COLOR="Red"]FIN DE LINEAS DE ERROR
[/COLOR]

*********************************************

        Catch ex As Exception
            MessageBox.Show("ERROR al guardar los datos:" & vbCrLf & _
                            ex.Message, "Error al guardar los datos", _
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        ' 
Agregar un nuevo elemento
        
' Se añade un nuevo registro y se muestra vacío
        Dim dr As DataRow = dt.NewRow
        dt.Rows.Add(dr)
        dtNavegador.ActualizarTabla(dt)
        dtNavegador.Ultima()
        actualizarNavegacion(True)
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click

    End Sub

    Private Sub BindingNavigatorMoveFirstItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorMoveFirstItem.Click
        dtNavegador.Primera()
        actualizarNavegacion(True)
    End Sub

    Private Sub BindingNavigatorMovePreviousItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorMovePreviousItem.Click
        dtNavegador.Anterior()
        actualizarNavegacion(True)
    End Sub

    Private Sub BindingNavigatorMoveNextItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorMoveNextItem.Click
        dtNavegador.Siguiente()
        actualizarNavegacion(True)
    End Sub

    Private Sub BindingNavigatorMoveLastItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorMoveLastItem.Click
        dtNavegador.Ultima()
        actualizarNavegacion(True)
    End Sub

    Private Sub ConFotosActualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConFotosActualizar.Click
        ' 
Actualizar los datos actuales en la fila actual
        Dim dr 
As DataRow dtNavegador.FilaActual
        
If dr Is Nothing Then
            dr 
dt.NewRow
            dt
.Rows.Add(dr)
            
dtNavegador.ActualizarTabla(dt)
        
End If
        
dr("Nombre") = Me.NombreTextBox.Text
        dr
("Foto") = TablaNavegar.Image2Bytes(Me.FotoPictureBox.Image)
    
End Sub

    
Private Sub btnExaminar_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles btnExaminar.Click
        
' Seleccionar la imagen
        Dim oFD As New OpenFileDialog
        oFD.Title = "Selecccionar la imagen"
        oFD.Filter = "Todos (*.*)|*.*|Imagenes|*.jpg;*.gif;*.png;*.bmp"
        If oFD.ShowDialog = Windows.Forms.DialogResult.OK Then
            ' 
La cantidad de caracteres máximo
            
' (por si el path es demasiado largo)
            Dim i As Integer = dt.Columns("Nombre").MaxLength
            If i < 0 Then i = 255
            ' 
El nombre del fichero
            
' Nos quedamos solo con el nombre, sin el path
            Dim sNombre As String = System.IO.Path.GetFileName(oFD.FileName)
            If sNombre.Length > i Then
                ' 
Si el nombre es más grande de lo permitidolo cortamos
                sNombre 
sNombre.Substring(0i)
            
End If
            
Me.NombreTextBox.Text sNombre
            Me
.FotoPictureBox.Image Image.FromFile(oFD.FileName)
        
End If
    
End Sub

    
Private Sub btnMostrar_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles btnMostrar.Click
        My
.Forms.FormMostrar.CadenaConexion cnnStr
        My
.Forms.FormMostrar.Show()
    
End Sub
End 
Class 


DE ANTEMANO GRACIAS POR LA AYUDA
  #2 (permalink)  
Antiguo 28/01/2009, 07:12
Avatar de wladtepes  
Fecha de Ingreso: febrero-2008
Mensajes: 140
Antigüedad: 16 años, 3 meses
Puntos: 0
Respuesta: Error en updatecommand

lo resolvi era que no habia definido una clave primaria en la base de datos
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 21:40.