Updating underlying table

I have a form based on a BindingSource. The text controls' Data Source Update Mode are set to OnPropertyChanged. The FormClosing method (I almost said "event") has the line

Me.nameBindingSource.EndEdit()

Changes to the data in the fields on the form are not saved when the form is closed. How can I do this



Answer this question

Updating underlying table

  • Shammi Jayaraj

    Again, I added a test form. This time I pointed to a different data table. The same error occurred.

    Here is the code for the test form:

    Public Class frmTest

    Private Sub TblTransferBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TblTransferBindingNavigatorSaveItem.Click

    Me.Validate()

    Me.TblTransferBindingSource.EndEdit()

    Me.TblTransferTableAdapter.Update(Me.ManForexCLSDataSet.tblTransfer)

    End Sub

    Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'TODO: This line of code loads data into the 'ManForexCLSDataSet.tblTransfer' table. You can move, or remove it, as needed.

    Me.TblTransferTableAdapter.Fill(Me.ManForexCLSDataSet.tblTransfer)

    End Sub

    End Class

    The error occurred on the Update line


  • Ashwani146552

    I'm the user. It doesn't work for me.

    I've noticed that none of the Microsoft.SQLServer.Server references are connected with the project. Would this make any difference


  • lindaonlytry

    Which line of code generates the error Are these three lines the only code in the OnClosing event handler

    You might try creating an intermediate tblSystem object and setting it equal to tblSystem.GetChanges(), then, using the debugger, see if the intermediate table contains any rows... Be sure that you're not calling AcceptChanges() anywhere after the data is edited.

    We may need to see more of the code involved, such as the Fill() code used to get the data initially and any update code that may exist.



  • Mehdi42

    I added a new form and dragged one of the fields from the DataSource onto it. I opened the form and changed the data. When I used the BindingNavigator to try to update that form, I got this error

    A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.dll

    System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>ManForexCLS.vshost.exe</AppDomain><Exception><ExceptionType>System.InvalidOperationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Update requires a valid UpdateCommand when passed DataRow collection with modified rows.</Message><StackTrace> at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)

    It came from this line in the code of the new form:

    Me.TblSystemTableAdapter.Update(Me.ManForexCLSDataSet.tblSystem)

    which was in the SaveItem_Click event or method or whatever you're calling it these days.


  • Uday94404

    I have reconfigured the TableAdapter by deleting it from the designer and adding it back from the Data Source window. I verified that the Data Source window was reflecting the current configuration from Server Explorer (always a problem). I made sure that the configuration was generating the update methods when I added it back to the designer.

    The line that is causing the error is the Update line (the third of the set). Here is the complete code behind that form (by the way, there is no other form that updates the dataset in the application):

    Private Sub frmSystem_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    Try

    Me.Validate()

    Me.TblSystemBindingSource.EndEdit()

    Me.TblSystemTableAdapter.Update(Me.ManForexCLSDataSet.tblSystem)

    Catch ex As Exception

    MessageBox.Show(ex.ToString)

    End Try

    End Sub

    Private Sub frmSystem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'TODO: This line of code loads data into the 'ManForexCLSDataSet.tblSystem' table. You can move, or remove it, as needed.

    Me.TblSystemTableAdapter.Fill(Me.ManForexCLSDataSet.tblSystem)

    End Sub

    Private Sub butReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butReport.Click

    frmSystemProcesses.Show()

    End Sub

    Private Sub butFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Me.TblSystemTableAdapter.Fill(Me.ManForexCLSDataSet.tblSystem)

    End Sub

    Private Sub buUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Try

    Me.TblSystemTableAdapter.Update(Me.ManForexCLSDataSet.tblSystem)

    Catch ex As Exception

    MessageBox.Show(ex.ToString)

    End Try

    End Sub


  • Joshua Flanagan

    No, You dont need those references at all.

    So you can confirm you definately got an update command established for the table adapter.

    The best way to check if the update method exists is to open the Dataset Designer by double-clicking on your dataset in the Solution Explorer.

    After the designer is open click on the TableAdapter in the designer and then look at the Properties Window. There is a property called "UpdateCommand".

    If the command was generated then you should be able to expand this property and see what the exact command was that you generated. If it wasn't generated the property should have a value of "Nothing".


    The dataset when generated by default will not auto generate the delete, update and insert methods unless you specifically ask for them. So this would be my first course of action based upon this exception.


  • RVlem

    I have resolved the problem. I had to delete the connection information from Project Settings and delete the data source. Then I had to build a new connection string and then a new data source. Now the system works properly.

    There's a real problem with the data source refresh. It doesn't pick up changes to the structure of the underlying server database. Maybe it's better with SServer 2005, but it makes a mess out of SServer 2000 projects.

    Thanks for all of your help. I appreciate the attention you've given to this.

    Tom


  • UNOTech

    Take a look at this info in the exception:

    Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

    Does your TableAdapter have an UpdateCommand The designer cannot autogenerate one under certain conditions, such as a query that pulls data from more than one table...



  • Donald D. Drake - MSFT

    There will still be a query involved in order to fill the DataTable (albeit a simple one: Select * From TableName).

    Here's a thought... Does the user whose credentials are being used to access the database have Write permissions



  • satish VSKP

    The table adapter is pointing to a single table, not to a query. When it was generated through the configuration wizard, I made sure that the update methods were being generated.


  • myoungbl

    The datasource is not dynamic with the database. I will create a typed dataset of what is present in the database at that time, yoi changed the database after that then the two will be out of sync until you delete and recreate the datasource.

    this is one reason i personally dont like than as they are only helpers - you need to understand what these designers are doing - they are helping you build a typed dataset linked to a database but it is a snapshot of what was present at that time.


  • Tanmoy007

    The BindingSource object allows binding and navigation of the underlying data.  There should also be an associated TableAdapter object.  You need to call the Update() method of the TableAdapter and pass it the updated data.

    (This is assuming you're bound to a database.  If not, what is your datasource )



  • barteknj

    I placed the following lines in the OnClosing method of my form:

    Me.Validate()

    Me.TblSystemBindingSource.EndEdit()

    Me.TblSystemTableAdapter.Update(Me.ManForexCLSDataSet.tblSystem)

    The data doesn't update. When the form closes, I generate this error:

    A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.dll


  • Igor I.I.

    I've tried building a new project. I created a new DataSource. I dragged one field to the new startup form.

    It still won't update the underlying data.


  • Updating underlying table