Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Copying rows from one table to another in Visual Basic 2010

Posted on 2010-11-25
7
Medium Priority
?
530 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
5 Comments
 
LVL 3

Accepted Solution

by:
pnedic earned 1000 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
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…
Integration Management Part 2

877 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