Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6372745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:16:34+00:00 2026-05-25T01:16:34+00:00

I am trying to update TableTwo using a DataTable built using TableOne . The

  • 0

I am trying to update TableTwo using a DataTable built using TableOne.
The relationship between tables is a foreign column called TableOneId inside TableTwo.

I used the following code sample to make this work: Performing Batch Operations Using DataAdapters (MSDN)

The DataTable is populated in another Public Shared function.

I can’t figure out what is wrong. No error messages are reported. The watch reveals that the DataTable is loaded with data.

The DataTable is defined as:

Public MyDataTable As New DataTable

Public Shared Sub DefineDataTable()

    Dim ErrorEmail As New ErrorEmailMessageClass
    With ErrorEmail
        Try
            Using connection As New SqlConnection(My.Settings.MyDB)
                MyDataTable.Columns.Add("ID", Type.GetType("System.Int32"))
                MyDataTable.Columns.Add("Column1", Type.GetType("System.Int32"))
                MyDataTable.Columns.Add("Column2", Type.GetType("System.Int32"))
                MyDataTable.Columns.Add("Column3", Type.GetType("System.Int32"))
                MyDataTable.Columns.Add("Column4", Type.GetType("System.Int32"))
            End Using
        Catch ex As Exception
            .WriteError("Sub DefineDataTable", ex.Message)
        End Try
    End With
End Sub

But the SqlDataAdapter is not updating:

Public Shared Sub UpdateTable()
    Dim ErrorEmail As New ErrorEmailMessageClass

    With ErrorEmail
        Try
            Using connection As New SqlConnection(My.Settings.MyDB)
                connection.Open()

                Dim adapter As New SqlDataAdapter()

                'Set the UPDATE command and parameters.
                adapter.UpdateCommand = New SqlCommand( _
                  "UPDATE Schema.TableTwo " _
                  & "SET " _
                  & "Column1=@Column1, " _
                  & "Column2=@Column2, " _
                  & "Column3=@Column3, " _
                  & "Column4=@Column4 " _
                  & "WHERE TableOneId=@ID;", connection)
                adapter.UpdateCommand.Parameters.Add("@Column1", SqlDbType.Int, 4, "Column1")
                adapter.UpdateCommand.Parameters.Add("@Column2", SqlDbType.Int, 4, "Column2")
                adapter.UpdateCommand.Parameters.Add("@Column3", SqlDbType.Int, 4, "Column3")
                adapter.UpdateCommand.Parameters.Add("@Column4", SqlDbType.Int, 4, "Column4")
                adapter.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int, 4, "ID")
                adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.OutputParameters

                ' Set the batch size.
                adapter.UpdateBatchSize = 0

                ' Execute the update.
                adapter.Update(MyDataTable)

                connection.Close()
            End Using
        Catch ex As Exception
            .WriteError("Sub UpdateTable", ex.Message)
        End Try
    End With
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:16:34+00:00Added an answer on May 25, 2026 at 1:16 am

    As you’ve commented in your other question, here is my answer:

    You should set the UpdateBatchSize property of the SqlDataAdapter to 0 (unlimited).
    I don’t see a way to update table2 without looping table1.

    Here is a sample code to show you one way to achieve this:

    Public Sub BatchUpdate(ByVal table1 As DataTable)
        Dim connectionStringServer2 As String = GetConnectionString()
    
        Using connection As New SqlConnection(connectionStringServer2)
            Dim adapter As New SqlDataAdapter()
    
            'Set the UPDATE command and parameters'
            adapter.UpdateCommand = New SqlCommand( _
              "UPDATE Table2 SET " _
              & "NAME=@NAME,Date=@Date  WHERE TableOneId=@TableOneId;", _
              connection)
            adapter.UpdateCommand.Parameters.Add("@Name", _
              SqlDbType.NVarChar, 50, "Name")
            adapter.UpdateCommand.Parameters.Add("@Date", _
              SqlDbType.DateTime, 0, "Date")
            adapter.UpdateCommand.Parameters.Add("@TableOneId", _
            SqlDbType.Int, 0, "TableOneId")
            adapter.UpdateCommand.UpdatedRowSource = _
              UpdateRowSource.None
    
            ' Set the batch size,' 
            ' try to update all rows in a single round-trip to the server'
            adapter.UpdateBatchSize = 0
            ' You might want to increase the UpdateCommand's CommandTimeout as well'
            adapter.UpdateCommand.CommandTimeout = 600 '10 minutes'
    
            Dim table2 As New DataTable("table2")
            table2.Columns.Add(New DataColumn("Name", GetType(String)))
            table2.Columns.Add(New DataColumn("Date", GetType(Date)))
            table2.Columns.Add(New DataColumn("TableOneId", GetType(Int32)))
    
            ' copy content from table1 to table2'
            For Each row As DataRow In table1.Rows
                Dim newRow = table2.NewRow
                newRow("TableOneId") = row("ID")
                newRow("Name") = row("Name")
                newRow("Date") = row("Date")
                table2.Rows.Add(newRow)    
                ' note: i have not tested following, but it might work or give you a clue'
                newRow.AcceptChanges()
                newRow.SetModified()
            Next
    
            ' Execute the update'
            AddHandler adapter.RowUpdated, _
            New SqlRowUpdatedEventHandler(AddressOf OnRowUpdated)
    
            adapter.UpdateBatchSize = 5000   
            adapter.UpdateCommand.CommandTimeout = 6000
            adapter.ContinueUpdateOnError = True                      
            adapter.Update(table2)
    
        End Using
    End Sub
    Private Shared Sub OnRowUpdated(sender As Object, args As SqlRowUpdatedEventArgs)
        If args.RecordsAffected = 0 Then
            args.Row.RowError = "Optimistic Concurrency Violation!"
            args.Status = UpdateStatus.SkipCurrentRow
        End If
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting a strange exception when trying to update a DataTable after flagging
I am trying update my table from c# ado.net with this function.I am using
Trying to update a table view using: CREATE OR REPLACE VIEW [vtable] AS SELECT
I'm trying to update a hashtable in a loop but getting an error: System.InvalidOperationException:
I got this error when trying to update an image. It was a cross-thread
Recently, my Eclipse 3.4 installation started complaining while trying to update installed software. I
I'm trying to safely update the home directory as specified in /etc/passwd , but
I'm trying to do a simple update. I've done this kind of thing thousands
I'm trying to do the classic Insert/Update scenario where I need to update existing
I'm trying to run an update query that updates one table based on rows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.