Solved

Copying rows from one table to another in Visual Basic 2010

Posted on 2010-11-25
7
522 Views
Last Modified: 2012-05-10
This is a pointer to question http://www.experts-exchange.com/Microsoft/Development/Q_26626345.html

can anyone help me?

Using Visual Basic 2010 I want to be abe to

1. Filter a table
2. Copy the selected row from one table to another

Please see question for more details
0
Comment
Question by:KCTS
  • 3
7 Comments
 
LVL 3

Accepted Solution

by:
pnedic earned 250 total points
ID: 34215899
Hello

If you want to select some row(s) from a table you can use DataSet.Tables(0).Select command:

Dim ds as DataSet = new DataSet()
'...define sql string, make connection to DB, create DataAdapter (as da) and do da.Fill(sql, DBconn)
da = New OleDb.OleDbDataAdapter(sqlStr, liveConnection)
da.Fill(ds)   'populate DataSet with data
Dim whDr() As DataRow
whDr = ds.Tables(0).Select("someColName = "+desiredValue)
Now you got array of rows that satisfies desired condition. Now you can use For loop to position only one row or one column value from one row as:
For i .....
      strValue = CStr(whDr(i).Item(colIndex))
Next

******

To copy one row to the other table, you can use previous logic to get data from onw row and that use INSERT INTO sql command to insert into second table:
INSERT INTO table2 (col1, col2, ... colX) VALUES (val1, val2, ... valX)

Insert command you will execute as :

Dim myTransaction As OleDb.OleDbTransaction
Dim myCommand As New OleDb.OleDbCommand
Try
       myCommand.Connection = liveConnection
       myTransaction = liveConnection.BeginTransaction()
       myCommand.Transaction = myTransaction
       myCommand.CommandText = sqlStr       'sqlStr is Insert sql command
       myCommand.CommandType = CommandType.Text
       myCommand.ExecuteNonQuery()
       myTransaction.Commit()
Catch ex As Exception
       myTransaction.Rollback()
Finally
       myCommand.Dispose()
       myCommand = Nothing
End Try

This is one approach. Other people will probably suggest something else.
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 250 total points
ID: 34218484
You can do this

dTable.DefaultView.RowFilter = "Type=1"
Dim dTemp as DataTable = dTable.DefaultView.ToTable()
0
 
LVL 3

Expert Comment

by:amittripathi8
ID: 34222297
just use importrows of datatable method which import rows
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34813026
You should know better being a qualified expert that you are supposed to provide feedback on suggestions.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34879675
Divide between http:# 34215899 and http:#34218484
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question