Results 1 to 7 of 7

Thread: Putting a datatable into a dataset problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    28

    Putting a datatable into a dataset problem

    I am trying to put a datatable into a dateset, which I then export to a CSV file. I am getting this error

    DataTable already belongs to another DataSet. on the following line dsExport.Tables.Add(dtExport)

    VB Code:
    1. Using dtExport As DataTable = DirectCast(dgvSafetyGlasses.DataSource, DataTable)
    2.             Using dsExport As New DataSet()
    3.                 dsExport.Tables.Add(dtExport)
    4.                 ExportDatasetToCsv(dsExport)
    5.             End Using
    6.         End Using

    What am I doing wrong?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Putting a datatable into a dataset problem

    As the error message suggests, you're trying to put the table into a DataSet when it's already in another DataSet. Why can't you use the existing DataSet? If there is a legitmate reason then you'll need to either remove the table from the original DataSet first or else create a copy to put in the new DataSet.

    That said, do you even need the DataSet? Can you not just export the DataTable to a CSV? I'm guessing that your export code only works on one table anyway so what's the DataSet actually for?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    28

    Re: Putting a datatable into a dataset problem

    I don't think I can use the existing Dataset because I use a filter on the grid and I only way to export the data that is currently in the grid. I did try to use the datatable as it was but I couldn't get the code right in the sub. Although I think I have it now. I will post back if I get it to work correctly.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Putting a datatable into a dataset problem

    If you're filtering and/or sorting data then you should be getting your data to export from the DataTable's DefaultView rather than from its Rows collection. Sorting and filtering affects only the DataView, not the DataTable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    28

    Re: Putting a datatable into a dataset problem

    It will not export after I filter the data out and I read where you said I need to use the default view but I am not exactly sure where to put it. I can put it where I have it commented out below but I can run and test because I error on this error Unable to cast object of type 'System.Data.DataView' to type 'System.Data.DataTable'. on this line dtExport As DataTable = DirectCast(dgvSafetyGlasses.DataSource, DataTable). I am pretty confident that this is the line I need to change but I haven't got it figured out quite yet.
    VB Code:
    1. '
    2.     Private Sub exportFromDatagrid()
    3.         If My.Computer.FileSystem.FileExists(FilePath) = True Then
    4.             My.Computer.FileSystem.DeleteFile(FilePath)
    5.         End If
    6.         '
    7.         Using dtExport As DataTable = DirectCast(dgvSafetyGlasses.DataSource, DataTable)
    8.             ExportDataTableToCsv(dtExport)
    9.         End Using
    10.  
    11.         My.Computer.FileSystem.WriteAllText(FilePath, str.ToString, True)
    12.         System.Diagnostics.Process.Start("notepad.exe", FilePath)
    13.     End Sub
    14.  
    15.     Private Sub ExportDataTableToCsv(ByVal dt As DataTable)
    16.         For Each dr As DataRow In dt.Rows 'dt.defaultview
    17.             For Each field As Object In dr.ItemArray
    18.                 str.Append(field.ToString & ",")
    19.             Next
    20.             str.Replace(",", vbNewLine, str.Length - 1, 1)
    21.         Next
    22.     End Sub

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    28

    Re: Putting a datatable into a dataset problem

    Should I continue with this and convert it into a Dataview
    VB Code:
    1. Using dtExport As DataTable = DirectCast(dgvSafetyGlasses.DataSource, DataTable)
    2.             ExportDataTableToCsv(dtExport)
    3.         End Using
    Or do I need to start all over with a Dataview as my first step (Datagridview > Dataview)?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Putting a datatable into a dataset problem

    Using a DataView is very easy. Every DataTable already has a DataView associated with it by default, which you access via its DefaultView property. In fact, when you bind a DataTable to a control, the data you see is actually from the DefaultView. That's how, for example, you're able to bind a DataTable to a DataGridView and sort it. A Datatable can't be sorted, but a DataView can.

    So, when binding, you should generally just go ahead and bind your DataTable. There's no need to bind the DefaultView because that's where the data comes from anyway. In your case, if you want to export the filtered and sorted data then you should rewrite your ExportDataTableToCsv method. Either change it to ExportDataViewToCsv and make the parameter type DataView, or else use the table's DefaultView inside the method. So, if your method was currently like this:
    vb.net Code:
    1. Private Sub ExportDataTableToCsv(ByVal table As DataTable)
    2.     Using writer As New IO.StreamWriter("file path here")
    3.         For rowIndex As Integer = 0 To table.Rows.Count - 1
    4.             Dim row As DataRow = table.Rows(rowIndex)
    5.  
    6.             'Write a line break before all but the first line.
    7.             If rowIndex > 0 Then
    8.                 writer.WriteLine()
    9.             End If
    10.  
    11.             For columnIndex As Integer = 0 To table.Columns.Count - 1
    12.                 'Write a comma before all but the first field.
    13.                 If columnIndex > 0 Then
    14.                     writer.Write(",")
    15.                 End If
    16.  
    17.                 If table.Columns(columnIndex).DataType Is GetType(String) Then
    18.                     'Quote text values.
    19.                     writer.Write("""{0}""", row(columnIndex))
    20.                 Else
    21.                     writer.Write(row(columnIndex))
    22.                 End If
    23.             Next
    24.         Next
    25.     End Using
    26. End Sub
    then you should change it to this:
    vb.net Code:
    1. Private Sub ExportDataViewToCsv(ByVal view As DataView)
    2.     Using writer As New IO.StreamWriter("file path here")
    3.         For rowIndex As Integer = 0 To view.Count - 1
    4.             Dim row As DataRowView = view(rowIndex)
    5.  
    6.             'Write a line break before all but the first line.
    7.             If rowIndex > 0 Then
    8.                 writer.WriteLine()
    9.             End If
    10.  
    11.             For columnIndex As Integer = 0 To view.Table.Columns.Count - 1
    12.                 'Write a comma before all but the first field.
    13.                 If columnIndex > 0 Then
    14.                     writer.Write(",")
    15.                 End If
    16.  
    17.                 If view.Table.Columns(columnIndex).DataType Is GetType(String) Then
    18.                     'Quote text values.
    19.                     writer.Write("""{0}""", row(columnIndex))
    20.                 Else
    21.                     writer.Write(row(columnIndex))
    22.                 End If
    23.             Next
    24.         Next
    25.     End Using
    26. End Sub
    or this:
    vb.net Code:
    1. Private Sub ExportDataTableToCsv(ByVal table As DataTable)
    2.     Using writer As New IO.StreamWriter("file path here")
    3.         For rowIndex As Integer = 0 To table.DefaultView.Count - 1
    4.             Dim row As DataRowView = table.DefaultView(rowIndex)
    5.  
    6.             'Write a line break before all but the first line.
    7.             If rowIndex > 0 Then
    8.                 writer.WriteLine()
    9.             End If
    10.  
    11.             For columnIndex As Integer = 0 To table.Columns.Count - 1
    12.                 'Write a comma before all but the first field.
    13.                 If columnIndex > 0 Then
    14.                     writer.Write(",")
    15.                 End If
    16.  
    17.                 If table.Columns(columnIndex).DataType Is GetType(String) Then
    18.                     'Quote text values.
    19.                     writer.Write("""{0}""", row(columnIndex))
    20.                 Else
    21.                     writer.Write(row(columnIndex))
    22.                 End If
    23.             Next
    24.         Next
    25.     End Using
    26. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width