Results 1 to 8 of 8

Thread: [2005] TableAdapter NOT Updating Database

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216

    Question [2005] TableAdapter NOT Updating Database

    Hello,

    I am using a strong typed dataset with tableadapters. I use the GetBy method to return a datatable from Oracle containing X amount of records.

    In my code I get a DataTable and then loop through the TableRows. All is well until I try to call the Update on my TableAdapter. I get a return code of 0 but no changes are made to my database.

    Any ideas?

    Thanks

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216

    Re: [2005] TableAdapter NOT Updating Database

    VB Code:
    1. Try
    2.             '--Turn off timer so we don't step on toes.
    3.             Timer1.Enabled = False
    4.             Dim IIIReleaseAdapter As New III_RELEASETableAdapter
    5.             Dim IIIReleaseDataTable As New IIIRelease.III_RELEASEDataTable
    6.             '--Get release data.
    7.             IIIReleaseDataTable = IIIReleaseAdapter.GetReleaseByImageSourceByStatusCode()
    8.             'IIIReleaseAdapter.FillByImageSourceByStatusCode(IIIReleaseDataTable)
    9.             For Each row As IIIRelease.III_RELEASERow In IIIReleaseDataTable.Rows
    10.                 '--Set up the feed class with the correct information.
    11.                 '--This also clears out any previous values.
    12.                 Dim MyFIS As New FISFeed
    13.                 With MyFIS
    14.                     .FIS_OLEDB_Connection = OleDbConnection1
    15.                     .LesseeNumber = row.MFINDEX1.ToString
    16.                     .LesseeName = row.MFINDEX2.ToString
    17.                     .DocumentType = row.BINDER_CATEGORY.ToString.Substring(0, 3)
    18.                     .DocScanDate = CType(row.SCAN_DATE, DateTime).ToString("yyyyMMdd")
    19.                     .DocScanTime = CType(row.SCAN_DATE, DateTime).ToString("hhmmss")
    20.                     .CDNumber = ""
    21.                     .TLSADate = row.BINDEX1.ToString
    22.                     .DocVerDate = row.BINDEX2.ToString
    23.                     .DocSignDate = row.BINDEX3.ToString
    24.                     .ScheduleStatus = row.BINDEX4.ToString
    25.                     .DocOrigFile = row.BINDEX5.ToString
    26.                     .ExpirationDate = row.BINDEX6.ToString
    27.                     .VehicleNumber = row.BINDEX1.ToString
    28.                 End With
    29.                 row.STATUS_CODE = "X"
    30.                 row.AcceptChanges()
    31.                 '--Don't want to insert in debug mode.
    32. #If Debug = False Then
    33.                 MyFIS.Insert()
    34. #End If
    35.             Next
    36.             IIIReleaseAdapter.Update(IIIReleaseDataTable)
    37.             '--Turn it back on.
    38.             Timer1.Enabled = True
    39.         Catch ex As Exception
    40.             MessageBox.Show(ex.Message.ToString)
    41.         End Try

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] TableAdapter NOT Updating Database

    You must be the millionth person to call AcceptChanges and then wonder why their update doesn't work. When you call AcceptChanges on a DataRow, DataTable or DataSet you clear all the flags that indicate that the data has changed. When you call Update only rows that are flagged as changed are processed. Do NOT call AcceptChanges explicitly in this situation. The default behaviour of the Update method is to implicitly call AcceptChanges after the changes have been saved. There are situations where it is necessary to call AcceptChanges explicitly but this is not one of them.

    To be detailed, a DataRow has two sets of data: original and current. When you add a new row the original data is blank and the RowState is Added. When you edit a row the original data is the same as it started and the current data contains the new data, while the RowState is Modified. When you delete a row the current data is blank and the RowState is Deleted. When you call AcceptChanges the values of the original data is overwritten by the values of the current data and the RowState is Unchanged. If the row was deleted it is removed from the table altogether. Now, when you call Update any rows with a RowState of Unchanged are ignored, so if you've called AcceptChanges on all rows then there are no changes in the table so nothing happens.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216

    Re: [2005] TableAdapter NOT Updating Database

    Quote Originally Posted by jmcilhinney
    You must be the millionth person to call AcceptChanges and then wonder why their update doesn't work. When you call AcceptChanges on a DataRow, DataTable or DataSet you clear all the flags that indicate that the data has changed. When you call Update only rows that are flagged as changed are processed. Do NOT call AcceptChanges explicitly in this situation. The default behaviour of the Update method is to implicitly call AcceptChanges after the changes have been saved. There are situations where it is necessary to call AcceptChanges explicitly but this is not one of them.

    To be detailed, a DataRow has two sets of data: original and current. When you add a new row the original data is blank and the RowState is Added. When you edit a row the original data is the same as it started and the current data contains the new data, while the RowState is Modified. When you delete a row the current data is blank and the RowState is Deleted. When you call AcceptChanges the values of the original data is overwritten by the values of the current data and the RowState is Unchanged. If the row was deleted it is removed from the table altogether. Now, when you call Update any rows with a RowState of Unchanged are ignored, so if you've called AcceptChanges on all rows then there are no changes in the table so nothing happens.
    Thanks for the reply:

    So what you are saying is that I should simply drop the
    VB Code:
    1. row.AcceptChanges()

    and all will be well?

    Also, after a little more investigation on this Oracle table, the DBA provided no primary key. That poses a problem doesn't it?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] TableAdapter NOT Updating Database

    There is no requirement to have a primary key in a table but it does simplify things quite a bit. When operating on a record it is generally important to be able to uniquely identify that record. Using a primary key is the most convenient way to do that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216

    Re: [2005] TableAdapter NOT Updating Database

    Quote Originally Posted by jmcilhinney
    There is no requirement to have a primary key in a table but it does simplify things quite a bit. When operating on a record it is generally important to be able to uniquely identify that record. Using a primary key is the most convenient way to do that.

    So then how does the code know how to uniquely identify a row?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] TableAdapter NOT Updating Database

    Quote Originally Posted by jesus4u
    So then how does the code know how to uniquely identify a row?
    If there's a way to uniquely identify the row by some column or combination of columns that aren't the primary key then you have to specify that. Most tables require that rows be uniquely identified and the easiest way to do that is with a primary key. If there is no primary key then you have to do more of the work yourself to write the data access code and specify the appropriate columns. If this table has been provided without a primary key without a good reason then that's just slack. If you need to be able to uniquely identify the rows and there is no way to do so then the database is useless and the person responsible needs a kick.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216

    Re: [2005] TableAdapter NOT Updating Database

    Quote Originally Posted by jmcilhinney
    If there's a way to uniquely identify the row by some column or combination of columns that aren't the primary key then you have to specify that. Most tables require that rows be uniquely identified and the easiest way to do that is with a primary key. If there is no primary key then you have to do more of the work yourself to write the data access code and specify the appropriate columns. If this table has been provided without a primary key without a good reason then that's just slack. If you need to be able to uniquely identify the rows and there is no way to do so then the database is useless and the person responsible needs a kick.
    Point taken ... so back to my code above. If I remove the row.AcceptChanges() call then the code will know what row I am referencing due to the fact of the For Loop?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width