DataTableReader is invalid for current DataTable  
Author Message
Alexandre Martins





PostPosted: .NET Framework Data Access and Storage, DataTableReader is invalid for current DataTable Top

I'm trying to convert a datatable into a datatablereader but i'm getting this error

DataTableReader is invalid for current DataTable

does everyboth nows about it

i'm running the simple command "

mydatatable.GetChanges(DataRowState.Added)

"




.NET Development11  
 
 
Figo Fei - MSFT





PostPosted: .NET Framework Data Access and Storage, DataTableReader is invalid for current DataTable Top

Hi, Mahesh819

You must do dataTableReader.Read(); first to get the record.

The code can be:

public void Execute()
{

using (DataTableReader dataTableReader = new DataTableReader(myDataTable))
{

if(dataTableReader.Read())

{
object[] values = new object[dataTableReader.FieldCount];

int fieldCount = dataTableReader.GetValues(values);
string id= values[4].ToString();
string Type = values[0].ToString();

}
}
}

If you have further problems, please feel free to let me know.

Thank you



 
 
Luis D. Rojas





PostPosted: .NET Framework Data Access and Storage, DataTableReader is invalid for current DataTable Top

Hi,

this is the definition from the msdn for the getChanges method

Gets a copy of the DataSet that contains all changes made to it since it was loaded or since AcceptChanges was last called.

Basically, it is fullfilled with only the changes that were done in the DataSet, in your case, where the RowState property of the columns is Added., so you can not get a DataReader from a Table using this method.

Then, remember that a DataReader is a connected object, so it needs a connection for working. so, you can not convert something is disconnected like a dataset and its datatables in a connected object.

What do you want to do

Best Regards



 
 
alazela





PostPosted: .NET Framework Data Access and Storage, DataTableReader is invalid for current DataTable Top

DataTableReader does provide a reader interface over a DataTable without needing a connection. The method to obtain the reader is DataTable.CreateDataReader().

The error you are seeing is thrown by the reader when the current row the reader is positioned on is not present or is invalid. This can happen if you make changes to the DataTable after obtaining the reader. It may also happen if you have no rows in the table when you call CreateDataReader() (I haven't actually tested this scenario, just a quick glance at the code).


 
 
Mahesh819





PostPosted: .NET Framework Data Access and Storage, DataTableReader is invalid for current DataTable Top

Thank you

Figo Fei - MSFT

Mahesh



 
 
Alexandre Martins





PostPosted: .NET Framework Data Access and Storage, DataTableReader is invalid for current DataTable Top

here is my situation...

I have to apply a filter....
Then i'm doing this...

DataTable blah = MyDataTable.Clone();
blah.ImportRow(MyDataRow);
blah.AcceptChanges();
DataTableReader Reader = blah.CreateDataReader();

When the method CreateDataReader is called the exception comes on my screen...

I'm lost...

Could you help me



 
 
Alexandre Martins





PostPosted: .NET Framework Data Access and Storage, DataTableReader is invalid for current DataTable Top

I found the problem !!!
I forgot the method Reader.Read();

tks everyone



 
 
musosdev





PostPosted: .NET Framework Data Access and Storage, DataTableReader is invalid for current DataTable Top

Thankyou.. I forgot the exact same thing! Surprise)