Results 1 to 5 of 5

Thread: [2005] "A DataTable named 'Stok' already belongs to this DataSet."

  1. #1

    Thread Starter
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    Question [2005] "A DataTable named 'Stok' already belongs to this DataSet."

    Hi;
    I try to get informations from an Access DB. There is tabcontrol. There are some tabstops. If you enter a tabstop it tries to update data in Datagridview. But at the first time you enter to tabstop it normally takes data from database. If you try to take data for a second time you need to do this:

    VB Code:
    1. Me.DGW.DataSource = Nothing
    2.         DS.Tables.Clear()
    3.         DS.DataSetName = "NewDataSet"
    4.  
    5.         CN.Close()
    6.  
    7.         CNConnect()
    8.         RS.Open("SELECT * FROM " & TbStok & Ek, CN, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
    9.  
    10.         TakeData()
    11.         ShowData()

    and in this function I get the error, which is written in the title:

    VB Code:
    1. Private Function TakeData() As Boolean
    2.        
    3.         DS.Tables.Add(TbStok)
    4.         OL.Fill(DS.Tables(TbStok), RS)
    5.         DS.DataSetName = CN.ConnectionString
    6.         DGW.DataSource = DS.Tables(TbStok)
    7.         Return True
    8.  
    9.     End Function
    But I clear the tables b4 using takedata function. There was a table with the same name from first taking data but in a second time I clear it b4 taking data again. What shall I do?
    Dim Me As Coder

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2005] "A DataTable named 'Stok' already belongs to this DataSet."

    1) Are you trying to use ADO or ADO.NET? Your code is using both. Pick one or the other and stick to it.

    2) Yes, you are clearing the TABLES.... not the dataset.... big difference. Instead of using .CLEAR, check to see if the table exists in the Dataset first, then remove the table from the collection. Then you can fill it and add it back it.....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    Re: [2005] "A DataTable named 'Stok' already belongs to this DataSet."

    for 1) Hmm but it works good exccept this
    for 2) it seems that it doesnt exists after clear function but it gives the error
    Dim Me As Coder

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

    Re: [2005] "A DataTable named 'Stok' already belongs to this DataSet."

    My question is "why remove the table in the first place". If you just call the Fill method of a DataAdapter and pass a DataSet and a table name then it will populate a DataTable with that name, creating it if it doesn't exist. If you want to get rid of existing data then just clear the contents of the table:
    VB Code:
    1. If myDataSet.Tables.Contains("TableName") Then
    2.     'Remove any existing data.
    3.     myDataSet.Tables("TableName").Clear()
    4. End If
    5.  
    6. 'Populate the specified table, creating if required.
    7. myDataAdapter.Fill(myDataSet, "TableName")
    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
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    Re: [2005] "A DataTable named 'Stok' already belongs to this DataSet."

    Thanks again... it worked great jmchil
    Dim Me As Coder

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