Solved

Copying rows from one table to another in Visual Basic 2010

Posted on 2010-11-25
7
526 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

734 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