Solved

Copying rows from one table to another in Visual Basic 2010

Posted on 2010-11-25
7
527 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

623 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