This project has moved. For the latest updates, please go here.

Query error when deleted rows in DataTable

Topics: Developer Forum, User Forum
Jan 11, 2008 at 4:25 PM
I have a DataTable with some rows in it.
Some of them are deleted rows (DataRowState = Deleted).

When I execute a query against the DataTable I get an error. The query is simple: SELECT * FROM TableName. The error is:

ColumnBinding.GetValue() threw an exception
Inner Exception:
Deleted row information cannot be accessed through the row.
at NQuery.Runtime.ExecutionPlan.TableIterator.Read()
at NQuery.Runtime.ExecutionPlan.StatisticsIterator.Read()
at NQuery.Runtime.ExecutionPlan.FilterIterator.Read()
at NQuery.Runtime.ExecutionPlan.StatisticsIterator.Read()
at NQuery.Runtime.ExecutionPlan.HashMatchIterator.Read()
at NQuery.Runtime.ExecutionPlan.StatisticsIterator.Read()
at NQuery.Runtime.ExecutionPlan.HashMatchIterator.Read()
at NQuery.Runtime.ExecutionPlan.StatisticsIterator.Read()
at NQuery.Runtime.ExecutionPlan.SortIterator.SortInput()
at NQuery.Runtime.ExecutionPlan.SortIterator.Read()
at NQuery.Runtime.ExecutionPlan.StatisticsIterator.Read()
at NQuery.Runtime.ExecutionPlan.StreamAggregateIterator.Open()
at NQuery.Runtime.ExecutionPlan.StatisticsIterator.Open()
at NQuery.Runtime.ExecutionPlan.ResultIterator.Open()
at NQuery.Query.ExecuteDataTable()

Has any run into this? Is there a workaround?
Thank you in advance
Coordinator
Jan 20, 2008 at 5:50 PM
Hi glykoudis,

thank for you interest in NQuery and sorry for the late answer.


I have a DataTable with some rows in it. Some of them are deleted rows (DataRowState = Deleted).
When I execute a query against the DataTable I get an error. The query is simple: SELECT * FROM TableName.


I can reproduce your problem. The code I have used is this one:

DataTable dataTable = new DataTable();
dataTable.Columns.Add("ID", typeof(int));
dataTable.Columns.Add("Name", typeof(string));
dataTable.Rows.Add(1, "Name1");
dataTable.Rows.Add(2, "Name2");
dataTable.Rows.Add(3, "Name3");
dataTable.AcceptChanges();
dataTable.Rows[1].Delete();
dataTable.TableName = "Test";
 
Query query = new Query();
query.Text = "SELECT * FROM Test";
query.DataContext.Tables.Add(dataTable);
 
DataTable resultDataTable = query.ExecuteDataTable();
resultDataTable.TableName = "Result";
resultDataTable.WriteXml(Console.Out);


Is there a workaround?

Of course this is a bug and I will fix it as soon as I can. Meanwhile, I suggest you to use the following work around. For this you will have to derive from the built-in DataTableBinding class and fix it's behavior:

public class FixedDataTableBinding  : DataTableBinding
{
	public FixedDataTableBinding(DataTable dataTable)
		: base(dataTable)
	{
	}
 
	public override IEnumerable GetRows(ColumnRefBinding[] neededColumns)
	{
		foreach (DataRow dataRow in base.GetRows(neededColumns))
		{
			if (dataRow.RowState != DataRowState.Deleted)
				yield return dataRow;
		}
	}
}

Instead of adding the data table directly to the data context

query.DataContext.Tables.Add(dataTable);

you now add the corrected data table binding:

query.DataContext.Tables.Add(new FixedDataTableBinding(dataTable));
Hope this helps.
Coordinator
Jan 20, 2008 at 5:54 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.