tags:

views:

471

answers:

1

This code giving me fits. Returns the error:

The given ColumnName 'COURSENAME' does not match up with any column in data source.

If I remove the mappings, nothing gets written to the db either. Help!

Dim connectionString As String = "Data Source=2UA72518QY\SQLEXPRESS;Integrated Security=True;Pooling=False;Initial Catalog='Foo_Content'"

Dim ds As New DataSet
Dim sourceData As New DataTable

'Populate Dataset with chosen XML
ds.ReadXml(Server.MapPath("xml/FOOSECTIONS.XML"))

'Gets the tables in the DataSet
sourceData = ds.Tables.Add

'Add the data
Using destinationConnection As SqlConnection = New SqlConnection(connectionString)

    destinationConnection.Open()

    'Start copying!
    Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(destinationConnection)

        bulkCopy.ColumnMappings.Add("COURSENAME", "COURSENAME")
        bulkCopy.ColumnMappings.Add("COURSETITLE", "COURSETITLE")
        bulkCopy.ColumnMappings.Add("COURSEDESC", "COURSEDESC")
        bulkCopy.ColumnMappings.Add("COURSETITLE", "COURSEFACULTY")
        bulkCopy.ColumnMappings.Add("COURSETERM", "COURSETERM")
        bulkCopy.DestinationTableName = "RISDCourseData"

        Try
            ' Write from the source to the destination.
            bulkCopy.WriteToServer(sourceData)

        Catch ex As Exception
            Response.Write(ex.Message)

        Finally
            ' Close the SqlDataReader. The SqlBulkCopy object is automatically closed
            ' at the end of the Using block.
            bulkCopy.Close()
            'Response.Redirect("Default.aspx")
        End Try

    End Using
End Using
+1  A: 

By using ds.Tables.Add you're using a new table for sourceData - try getting the table that actually got loaded? In C#, ds.Tables[0] (don't ask me VB... possibly ds.Tables(0))

Marc Gravell
Brilliant! Thank you!
mmcglynn