Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it L'avatar di Jupy64
    Registrato dal
    Sep 2004
    Messaggi
    1,151

    [VB.NET 2005] Modifica record non recepita dal DataAdapter

    Ciao a tutti, in un form sto usando degli userControl per avere una gestione/visualizzazione dati a più pagine ,con opzioni diverse.
    Per fare questo ho seguito l'esempio di alka (http://forum.html.it/forum/showthrea...ht=usercontrol), adattandolo alle mie esigenze.
    In una di questa pagine (userControl) ho popolato una dataGridWiew e quando vado a modificare (o aggiungere) una riga, il recordset sottostante non rileva nessuna modifica, e pertanto l'Update del Dataset non fa niente.
    Posto parte del codice che uso:

    codice:
    'CODICE NEL FORM INIZIALE
    
    Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM TipoPratica;", cn)
    Dim ds As New DataSet
    Dim WithEvents bs As New BindingSource
    
    da.Fill(ds,paginaAttuale.GetNomeTabella)
    bs.DataSource = ds
    bs.DataMember = paginaAttuale.GetNomeTabella
    
    'paginaAttuale corrisponde all'userControll attualmente caricato nel form..
    paginaAttuale.SetBinding(bs)
    
    Private Function AggiornaRecord() As Int32
        Dim retVal As Int32 = -1
        Dim cmdBuilder As New OleDb.OleDbCommandBuilder(da)
        Dim tr As OleDb.OleDbTransaction
        cn.Open()
        da.UpdateCommand = cmdBuilder.GetUpdateCommand
        da.InsertCommand = cmdBuilder.GetInsertCommand
        da.DeleteCommand = cmdBuilder.GetDeleteCommand
        tr = cn.BeginTransaction(IsolationLevel.ReadCommitted)
        da.UpdateCommand.Transaction = tr
        da.InsertCommand.Transaction = tr
        da.DeleteCommand.Transaction = tr
        Try
            retVal = da.Update(ds, paginaAttuale.GetNomeTabella)
            tr.Commit()
        Catch eUpdate As System.Exception
            MessageBox.Show("Errore nell'aggiornamento dei dati!" & Convert.ToChar(10) & "Errore: " & eUpdate.Message, "Aggiornamento dati", MessageBoxButtons.OK, MessageBoxIcon.Error)
            tr.Rollback()
            retVal = -1
        Finally
            cn.Close()
        End Try
        cmdBuilder.Dispose()
        tr.Dispose()
        Return retVal
    End Function
    
    
    'CODICE NELLA PAGINA (USERCONTROLL) ATTUALMENTE CARICATA
    
    Public Overrides Sub SetBinding(ByVal bs As System.Windows.Forms.BindingSource)
        dbGrid.DataSource = bs
    End Sub
    
    Public Overrides Function SetStatoTabella(ByVal stato As eStatoTabPagina) As eStatoTabPagina
    
        If stato = eStatoTabPagina.eInserimento Then
            .....
        ElseIf stato = eStatoTabPagina.eModifica Then
            idxRowMod = dbGrid.CurrentRow.Index
            dbGrid.CurrentRow.ReadOnly = False
            oldStyle = dbGrid.CurrentRow.InheritedStyle
            dbGrid.CurrentRow.DefaultCellStyle.SelectionBackColor = Color.Yellow
            dbGrid.CurrentRow.DefaultCellStyle.SelectionForeColor = Color.Red
            dbGrid.CurrentRow.DefaultCellStyle.ForeColor = Color.Red
            dbGrid.CurrentRow.Cells(1).Selected = True
            ......
        End If
        Return statoTabella
    End Function
    
    Public Overrides Function CheckDatiOk() As Boolean
        Dim msgErr As String = ""
        If dbGrid.Rows(idxRowMod).Cells(1).Value.ToString.Trim.Length < 2 Then
            msgErr = "La descrizione deve contenere almeno due caratteri."
        End If
        If Not msgErr = "" Then
            MessageBox.Show(msgErr, "Convalida dati", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        Else
            dbGrid.Rows(idxRowMod).Cells(1).Value = dbGrid.Rows(idxRowMod).Cells(1).Value.ToString.ToUpper()
            Return True
        End If
    End Function
    Quando devo modificare un record richiamo paginaAttuale.SetStatoTabella(eStatoTabPagina.eMod ifica)
    e per salvare:
    codice:
    If paginaAttuale.CheckDatiOk() Then
            If AggiornaRecord() > -1 Then
                paginaAttuale.SetStatoTabella(eStatoTabPagina.eLettura)
                MessageBox.Show("Record aggiornato.", "Aggiornamento record", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
    End If
    In fase di debug, ho notato che il valore che vado a modificare nella riga del datagrid, non viene visto come modificato, ma bensi rimane il valore originale..
    Ad esempio, se nella colonna Descrizione c'è "ESEMPIO" e io lo modifico in "ESEMPLARE" in fase di debug vedo che il valore della cella modificata rimane "ESEMPIO".. non capisco il motivo.
    Mentre se cancello un record (codice sottostante) tutto procede bene.
    codice:
    bs.RemoveCurrent()
    If AggiornaRecord() > -1 Then
        MessageBox.Show("Record cancellato.", "Cancellazione record", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Else
        ds.Tables(paginaAttuale.GetNomeTabella).RejectChanges()
    End If
    Da premettere che tutto il codice, e relativi oggetti, è simile ad un'altro form (senza usercontrol) dove tutto funziona a meraviglia..
    Spero che qualcuno mi sappia dare qualche dritta..
    Grazie
    Jupy

  2. #2
    Utente di HTML.it L'avatar di Jupy64
    Registrato dal
    Sep 2004
    Messaggi
    1,151
    up.. :master:
    Jupy

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.