Copying a dataset to another dataset

I have an excel spreadsheet that I need to upload to a sql server database. The users of the spreadsheet may or may not change the columnheaders so I created a table in the db called importmap. the db table columns will not change but the spreadsheet's may.

So, I can get the spreadsheet into a datatable. My problem is now that I loop through the spreadsheet using the mapper I get an error after trying to add the spreadsheet's first row.

Here's my code. Could someone tell me what I'm doing wrong?

PrivateSub UploadData()

Try

'Setting the form

Dim xlConnStrAsString

Dim xlFileAsString

Dim xlSheetAsString

Dim xlSelectAsString

xlFile =Me.ExcelFileName1.Text

xlConnStr ="Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source=" & xlFile &";" & _

"Extended Properties=""Excel 8.0;" & _

"HDR=YES;IMEX=1"""

xlSheet =Me.WorksheetsCombobox.SelectedItem.ToString

xlSelect ="SELECT * FROM [" & xlSheet &"$]"

'Setting the connection

Dim xlConnAsNew System.Data.OleDb.OleDbConnection(xlConnStr)

Dim xlAdapterAsNew System.Data.OleDb.OleDbDataAdapter(xlSelect, xlConn)

Dim xlDataSetAsNew System.Data.DataSet

xlConn.Open()

xlAdapter.Fill(xlDataSet)

'Depending on which Dataset is being used will determine how to read XL into it.

Dim CurrentDateTimeAsDate = System.DateTime.Now

Dim TblMapAs DataTable =Me.DSScheduleTables.ImportMap

Dim DTableAs DataTable =Me.DSScheduleTables.Disbursements

Dim RTableAs DataTable =Me.DSScheduleTables.Receipts

Dim TTableAs DataTable =Me.DSScheduleTables.Transporter

Dim XTableAs DataTable = xlDataSet.Tables(0)

Dim DFilterAsString ="TableName = 'Disbursements'"

Dim RFilterAsString ="TableName = 'Receipts'"

Dim TFilterAsString ="TableName = 'Transporter'"

Dim NewDRowAs DataRow = DTable.NewRow()

IfMe.RadioButton1.Checked =TrueThen

'Import Disbursements using the mapper.

ForEach XTableRowAs DataRowIn XTable.Rows

ForEach MapRowAs DataRowIn TblMap.Select(DFilter)

If MapRow("Uploadcolumn")IsNothingOr IsDBNull(MapRow("UploadColumn"))Then

NewDRow(MapRow("TableColumn")) = MapRow("UploadConstant")

Else

NewDRow(MapRow("TableColumn")) = XTableRow(MapRow("UploadColumn"))

EndIf

Next

'Now add to the Disburment Table the new row

DTable.Rows.Add(NewDRow) < Errors here "This row already belongs to this table.

Next

EndIf

Catch exAs Exception

MsgBox(ex.Message)

EndTry

EndSub

When I view the table it is still blank, so not even the first row has been written to it.

[5885 byte] By [gregw1906] at [2008-1-10]
# 1

I had the same problem. I added the rows I was collecting into an arraylist. Once all my loops were complete then add/remove rows.

Dim List As New ArrayList

List.Add(Row)

After the loops were complete I did a loop to add/remove the rows

For Each Row in List.....add/remove

Hope this helps

Chris K.

Ckiszka at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Hi Gregw1906,

Based on your post, my understanding of your question is that when you add a new datarow to a datatable, the error occurs. It says "this row already belongs to this table".

Does the datatable object have the primarykey or unique datacolumn? Please check whether or not the primarykey or unique constraint produces this kind of the question. When the datatable has the primarykey or some unique datacolumns, if you add the dupplicate datarows to it, this will produce the similar error.

Best regards,

Riquel.

RiquelDong–MSFT at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

Hi,

the error is that you only instantiate one data row, then try to use it many times in your loop. Instead of

Dim NewDRow As DataRow = DTable.NewRow()

For Each XTableRow

you should have

Dim NewDRow As DataRow;

For Each XTableRow

NewDRow = DTable.NewRow()

...

Hope this helps,

Dag

dagjo at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...