DataTableReader is invalid for current DataTable

Posted by SheoNarayan under Error and Solution on 10/13/2010 | Points: 10 | Views : 8507 | Status : [Administrator] | Replies : 1

I was getting this error when accessing the DataTableReaedere records (Read about it http://www.dotnetfunda.com/articles/article987-working-with-datatablereader-class-.aspx ).

DataTableReader is invalid for current DataTable

I noticed that because I was not accessing the records under the loop of while(reader.Read()) I was getting this error. If only one record exists, you can call reader.Read() and then access the record without putting it inside the loop.

Hope this helps someone.

Regards,
Sheo Narayan
http://www.dotnetfunda.com



Responses

Posted by: Lalji_mer on: 10/14/2010 [Member] Bronze | Points: 25

Up
0
Down
Youcan try to do dataTableReader.Read(); first to get the record.

show here code sample


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();

}
}
}

Another example here
http://geekswithblogs.net/VROD/archive/2007/03/21/109416.aspx

LP MER

SheoNarayan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response