hey everybody,
i am new to vb.net environment pls help me to sort out my problem. let me explain it.
in my project i need to clone/copy the content of the two fields from one table to another table in same database.i have tried my level best but got the error in coding. can anuyone solve it pls. its vry urgent.

        Dim tblProducts As DataTable
        Dim tblProductsCopy As DataTable
        Dim tblProductsCount, tblProductsCopyCount, i As Integer
        conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Module2.accdb")
        Dim da As OleDbDataAdapter 'Create the DataAdapter.
        da = New OleDbDataAdapter("Select Itemname,ItemCode from sub", conn)
        Dim ds As DataSet = New DataSet("Products")

        da.Fill(ds, "products")
        ds.Tables.Add(tblProducts)

        tblProductsCount = tblProducts.Rows.Count


        tblProductsCopy = tblProducts.Clone()


        tblProductsCopy.ImportRow(tblProducts)

        tblProductsCopyCount = tblProductsCopy.Rows.Count

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

Dani AI

Generated

Quick, practical follow‑up with two reliable ways to copy the two columns you mentioned. As pointed out, doing the copy inside the database is usually the simplest and fastest. If you need to transform or validate rows first, copy into a DataTable, adjust, then push the rows back.

Database-side (recommended for bulk copy — runs entirely on the engine, no row-by-row roundtrips):

Using conn As New OleDbConnection(connString)
  conn.Open()
  Using tx = conn.BeginTransaction()
    Using cmd As New OleDbCommand(
      "INSERT INTO TargetTable ([ItemName],[ItemCode]) SELECT [ItemName],[ItemCode] FROM Sub WHERE [Flag]=?", conn, tx)
      cmd.Parameters.AddWithValue("?", filterValue) 'OleDb uses positional parameters
      cmd.ExecuteNonQuery()
    End Using
    tx.Commit()
  End Using
End Using

In-memory copy (when you must manipulate data in code first):

Dim ds As New DataSet()
adapter.Fill(ds, "source")
Dim src As DataTable = ds.Tables("source")   ' assign after Fill
Dim copyWithData As DataTable = src.Copy()   ' schema + rows

' or: copy schema then import rows correctly
Dim schemaOnly As DataTable = src.Clone()
For Each dr As DataRow In src.Rows
  schemaOnly.ImportRow(dr)
Next

Troubleshooting tips: make sure you assign the DataTable from the DataSet after Fill (missing that causes null errors), call ImportRow with a DataRow (not a DataTable), and exclude autonumber PK columns from the INSERT list. Use Using blocks and a transaction for safety, and prefer the DB-side INSERT...SELECT for large sets to avoid slow client-side loops.

Recommended Answers

All 2 Replies

Something like this would do the trick:

"INSERT INTO destinationTable(Itemname,Itemcode)" & _
  "SELECT sourceTable.ItemName, sourceTable.ItemCode " & _
   "FROM sourcetable WHERE sourcetable.Column=Value"

hey thanks...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.