[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:
Me.DGW.DataSource = Nothing
DS.Tables.Clear()
DS.DataSetName = "NewDataSet"
CN.Close()
CNConnect()
RS.Open("SELECT * FROM " & TbStok & Ek, CN, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
TakeData()
ShowData()
and in this function I get the error, which is written in the title:
VB Code:
Private Function TakeData() As Boolean
DS.Tables.Add(TbStok)
OL.Fill(DS.Tables(TbStok), RS)
DS.DataSetName = CN.ConnectionString
DGW.DataSource = DS.Tables(TbStok)
Return True
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?
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
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
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:
If myDataSet.Tables.Contains("TableName") Then
'Remove any existing data.
myDataSet.Tables("TableName").Clear()
End If
'Populate the specified table, creating if required.
myDataAdapter.Fill(myDataSet, "TableName")
Re: [2005] "A DataTable named 'Stok' already belongs to this DataSet."
Thanks again... it worked great jmchil