Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problema con TableAdapterManager.UpdateAll()

44 views
Skip to first unread message

LucaLinari

unread,
Nov 3, 2009, 9:39:03 AM11/3/09
to
Buongiorno,

Ho sviluppato una semplice applicazione database per verificare le
funzionalità del TableAdapterManager (Visual Studio 2008 – Framework 3.5 –
linguaggio VB); la mia applicazione si connette ad un database Access DB e ho
usato un DataSet Tipizzato (“DBRelDataSet”).
La mia applicazione ha 2 tabelle correlate “Autori” (Tabella Padre) e
“Libri” (Tabella Figlia) e le ho trascinate dentro un Form (“Form1”) così
appaiono come DataGridView.

La prima cosa che volevo fare era quella di recuperare il valore contatore
di Access (Autonumber) ogni volta che l’ utente inserisce una nuova riga nel
DataGridView (“Autori” o “Libri”). Per fare questo ho scritto le seguenti
righe di codice nell’ evento DataGridView_UserAddedRow:

Private Sub LibriDataGridView_UserAddedRow(ByVal sender As Object, ByVal e
As System.Windows.Forms.DataGridViewRowEventArgs) Handles
LibriDataGridView.UserAddedRow
Try
Me.Validate()
Me.AutoriBindingSource.EndEdit()
Me.LibriBindingSource.EndEdit()

Dim LibriGetChanges As DataTable =
DBRelDataSet.Libri.GetChanges(DataRowState.Added)

If Myconnection.State = ConnectionState.Closed Then
Myconnection.Open()
End If

LibriTableAdapter.Connection = Myconnection
LibriTableAdapter.Update(LibriGetChanges)

DBRelDataSet.Libri.Merge(LibriGetChanges)

Myconnection.Close()

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

Private Sub AutoriDataGridView_UserAddedRow(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles
AutoriDataGridView.UserAddedRow
Try
Me.Validate()
Me.AutoriBindingSource.EndEdit()
Me.LibriBindingSource.EndEdit()

Dim AutoriGetChanges As DataTable =
DBRelDataSet.Autori.GetChanges(DataRowState.Added)

If Myconnection.State = ConnectionState.Closed Then
Myconnection.Open()
End If

AutoriTableAdapter.Connection = Myconnection
AutoriTableAdapter.Update(AutoriGetChanges)

DBRelDataSet.Autori.Merge(AutoriGetChanges)

Myconnection.Close()

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

E poi:

Namespace DBRelDataSetTableAdapters

Partial Public Class LibriTableAdapter

Private Sub _adapter_RowUpdated(ByVal sender As Object, ByVal e As
System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles _adapter.RowUpdated
If e.StatementType = StatementType.Insert Then
Dim cmdGetIdentity As New OleDbCommand("SELECT @@IDENTITY",
Myconnection)
e.Row("IDlibro") = CInt(cmdGetIdentity.ExecuteScalar)
e.Status = UpdateStatus.SkipCurrentRow
End If
End Sub

End Class

Partial Public Class AutoriTableAdapter

Private Sub _adapter_RowUpdated(ByVal sender As Object, ByVal e As
System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles _adapter.RowUpdated
If e.StatementType = StatementType.Insert Then
Dim cmdGetIdentity As New OleDbCommand("SELECT @@IDENTITY",
Myconnection)
e.Row("IDautore") = CInt(cmdGetIdentity.ExecuteScalar)
e.Status = UpdateStatus.SkipCurrentRow
End If
End Sub

End Class

Il mio secondo intento era quello di salvare tutte le modifiche fatte alle
tabelle; così nel BindingNavigatorSaveItem_Click ho scritto le seguenti righe
di codice:

Private Sub AutoriBindingNavigatorSaveItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
AutoriBindingNavigatorSaveItem.Click
Try
Me.Validate()
Me.AutoriBindingSource.EndEdit()
Me.LibriBindingSource.EndEdit()

If Myconnection.State = ConnectionState.Closed Then
Myconnection.Open()
End If

Me.TableAdapterManager.Connection = Myconnection
Me.TableAdapterManager.UpdateAll(Me.DBRelDataSet)

Myconnection.Close()

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

A questo punto ho 2 domande:

1) Per ottenere il valore del contatore di Access (Autonumber), ogni volta
che l’ utente inserisce una nuova riga, ho implementato l’ evento
DataGridView_UserAddedRow. Qualcuno può dirmi se questo sistema è corretto e
“stabile” o ci sono altri modi migliori e più efficienti ?
2) Quando per salvare le modifiche clicco sul BindingNavigatorSaveItem
ottengo il seguente errore se in precedenza l’ utente ha inserito una nuova
riga in un DataGridView:
“Concurrency violation: the UpdateCommand affected 0 of the expected 1
records”
Non ho idea di come poter risolvere il problema o da cosa possa essere
causato ! qualcuno può aiutarmi ?

Grazie!
Luca

--
Luca Linari

0 new messages