Sunday, March 04, 2007

DataTable already belongs to another DataSet & DataRow already belongs to another DataRow.

Error: DataTable already belongs to another DataSet
Try in this way to copy data table from data set to another data set.
DataSet ds = new DataSet();
ds = GetDataSet();
ds.Tables[0].TableName = "Old";
DataSet dsNew = new DataSet();
DataTable dt = ds.Tables[0];
ds.Tables.Remove(dt);
dt.TableName = "New";
dsNew.Tables.Add(dt);
Error : DataRow already belongs to another DataRow.
Copy data Row from one data table to another data table
DataTable dt = new DataTable();
Use ImportRow Instead of Row.Add (dt.Rows.Add(dataRow);)
dt.ImportRow(dataRow);
Get Updated Row state
This GetChanges method will give the updated row from original dataset.
And RowState method will give the state “Updated”,“Deleted”,”Inserted”

dsDataSet.GetChanges()
dsDataSet.Table[0].GetChages()
string updateMode = dsDataSet.Tables[0].Rows[rowNumber].RowState.ToString();
Create DataSet from DataRow.
private DataSet GetFilterChildRecords(DataSet dsMainDataSet,string whereCondition)
{
try
{
DataRow[] dr = dsMainDataSet.Tables[1].Select(whereCondition);
dsSetActCode_Details = dsMainDataSet.Clone();
for (int i = 0 ;i<dr.Length ;i++)
{
DataRow drNew = dsSetActCode_Details.Tables[1].NewRow();
dsSetActCode_Details.Tables[1].Rows.Add(drNew);
for (int j = 0; j < dr[i].ItemArray.Length; j++)
{
dsSetActCode_Details.Tables[1].Rows[i][j] = dr[i][j];
}
}
return dsSetActCode_Details;
}
catch(Exception Ex)
{
throw Ex;}
}

No comments: