DataTable already belongs to another DataSet

后端 未结 10 1080
滥情空心
滥情空心 2020-12-24 11:14

This error is occuring while adding one datatable from a dataset to another .\"DataTable already belongs to another DataSet.\"

dsformulaValues.Tables.Add(m_D         


        
相关标签:
10条回答
  • 2020-12-24 11:38

    I found one turn around I hope it can help

    _DataTable.TableName = _TableName
    If _DataTable.DataSet IsNot Nothing Then
        _DataSet = _DataTable.DataSet
    Else
        _DataSet = New DataSet
        _DataSet.Tables.Add(_DataTable)
    End If
    

    Somehow even if _DataTable is new, but if it refers to a previous loaded physical table it becomes that DataTable inheriting the previous configuration.

    0 讨论(0)
  • 2020-12-24 11:42

    The accepted answer isn't very good. Cloning should always be a last option.

    Here's a way around the problem without incurring the overhead of cloning.

            DataSet ds = GetData1();
    
            DataSet ds2 = GetData2();
    
            //Assuming you know you've got good data
    
                DataTable dt = ds2.Tables[0];
                ds2.Tables.Remove(dt);
                dt.TableName = "PortedTable";//you may need to change the table name to prevent conflicts
                ds.Tables.Add(dt);
    
    0 讨论(0)
  • 2020-12-24 11:44

    dtCopy = dataTable.Copy()

    ds.Tables.Add(dtCopy)

    We can Optimize the code like :

    ds.Tables.Add(dataTable.Copy());

    0 讨论(0)
  • 2020-12-24 11:45

    The simple way is just merge the table as follows.

    dsformulaValues.Merge(m_DataAccess.GetFormulaValues(dv.ToTable.DefaultView.ToTable(False, strSelectedCols)).Tables(0))

    0 讨论(0)
提交回复
热议问题