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 formDim xlConnStrAsStringDim xlFileAsStringDim xlSheetAsStringDim xlSelectAsStringxlFile =
Me.ExcelFileName1.TextxlConnStr =
"Provider=Microsoft.Jet.OLEDB.4.0;" & _"Data Source=" & xlFile &";" & _"Extended Properties=""Excel 8.0;" & _"HDR=YES;IMEX=1"""xlSheet =
Me.WorksheetsCombobox.SelectedItem.ToStringxlSelect =
"SELECT * FROM [" & xlSheet &"$]"'Setting the connectionDim xlConnAsNew System.Data.OleDb.OleDbConnection(xlConnStr)Dim xlAdapterAsNew System.Data.OleDb.OleDbDataAdapter(xlSelect, xlConn)Dim xlDataSetAsNew System.Data.DataSetxlConn.Open()
xlAdapter.Fill(xlDataSet)
'Depending on which Dataset is being used will determine how to read XL into it.Dim CurrentDateTimeAsDate = System.DateTime.NowDim TblMapAs DataTable =Me.DSScheduleTables.ImportMapDim DTableAs DataTable =Me.DSScheduleTables.DisbursementsDim RTableAs DataTable =Me.DSScheduleTables.ReceiptsDim TTableAs DataTable =Me.DSScheduleTables.TransporterDim 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.RowsForEach MapRowAs DataRowIn TblMap.Select(DFilter)If MapRow("Uploadcolumn")IsNothingOr IsDBNull(MapRow("UploadColumn"))ThenNewDRow(MapRow(
"TableColumn")) = MapRow("UploadConstant")ElseNewDRow(MapRow(
"TableColumn")) = XTableRow(MapRow("UploadColumn"))EndIfNext'Now add to the Disburment Table the new rowDTable.Rows.Add(NewDRow) < Errors here "This row already belongs to this table.
NextEndIfCatch exAs ExceptionMsgBox(ex.Message)
EndTryEndSubWhen I view the table it is still blank, so not even the first row has been written to it.

