Click here to Skip to main content
15,911,474 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
's return error message :
DataTable already belongs to this DataSet.

At this line :
VB
ds.Tables.Add(Tblsheet)


VB
Dim SourceBookPath As String = Nothing
        Me.OpenFileDialog1.Filter = "Excel files (*.xls,*.xlsx)|*.xls;*.xlsx"
        Me.OpenFileDialog1.ShowDialog()
        If Not Me.OpenFileDialog1.FileName Is Nothing Then
            SourceBookPath = Me.OpenFileDialog1.FileName
        End If
        '---------------------------------------------------------------------------------
        Dim i As Integer
        Dim j As Integer
        Dim MsExcel1 = CreateObject("Excel.Application")
        Dim ExcelBook1 = MsExcel1.Workbooks.Open(SourceBookPath)
        Dim xlws = ExcelBook1.worksheets(1)

        Dim Tblsheet As New Data.DataTable
        Tblsheet.TableName = "Sheet1"
        Dim ds As New DataSet
        For c As Integer = 0 To 7
            Dim col As New Data.DataColumn
            col.ColumnName = xlws.Cells(14, c + 1).Value.ToString
            Tblsheet.Columns.Add(col)
            ds.Tables.Add(Tblsheet)
        Next

        '===========================================================

        Dim lastRow As Long = 0
        lastRow = MsExcel1.Range("A14").Find("*", MsExcel1.Range("A14"), XlFindLookIn.xlValues, , XlSearchOrder.xlByRows, XlSearchDirection.xlPrevious).row
        For i = 15 To lastRow
            Dim r As DataRow = ds.Tables("Sheet1").NewRow
            For j = 1 To 8 
                r(j - 1) = xlws.Cells(i, j).Value
            Next j
            ds.Tables("Sheet1").Rows.Add(r)
        Next i
        DataGridView1.DataSource = ds.Tables("Sheet1")
        '==========================================================
        Try
            TextBox1.Text = MsExcel1.Cells(6, 2).Value
            TextBox2.Text = MsExcel1.Cells(6, 4).Value
            DateTimePicker1.Text = MsExcel1.Cells(6, 6).Value
            TextBox4.Text = MsExcel1.Cells(6, 8).Value
            TextBox5.Text = MsExcel1.Cells(8, 2).Value
            DateTimePicker2.Text = MsExcel1.Cells(8, 4).Value
            TextBox7.Text = MsExcel1.Cells(8, 6).Value
            TextBox8.Text = MsExcel1.Cells(8, 8).Value
            DateTimePicker3.Text = MsExcel1.Cells(11, 2).Value
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        ExcelBook1.Close(False)
        MsExcel1.Quit()
        xlws = Nothing
        ExcelBook1 = Nothing
        MsExcel1 = Nothing
Posted
Comments
ZurdoDev 12-Jul-12 8:13am    
The error tells you what is wrong. What have you tried?

VB
For c As Integer = 0 To 7
    Dim col As New Data.DataColumn
    col.ColumnName = xlws.Cells(14, c + 1).Value.ToString
    Tblsheet.Columns.Add(col)
    ds.Tables.Add(Tblsheet)
Next


Looking at this piece of code, how many times does the table Tblsheet gets added?



Try adding the datatable to the dataset after you are finished filling the datatable with data.
 
Share this answer
 
Sir,

Move the error line out of the loop it will work..

VB
For c As Integer = 0 To 7
            Dim col As New Data.DataColumn
            col.ColumnName = xlws.Cells(14, c + 1).Value.ToString
            Tblsheet.Columns.Add(col)
            ds.Tables.Add(Tblsheet)
Next


Change To

VB
For c As Integer = 0 To 7
            Dim col As New Data.DataColumn
            col.ColumnName = xlws.Cells(14, c + 1).Value.ToString
            Tblsheet.Columns.Add(col)
        Next
            ds.Tables.Add(Tblsheet)
 
Share this answer
 
Thank you very much, Every thing is working now
Thanks A lot...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900