Click here to Skip to main content
15,912,897 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I want to fetch data from a database and retrieve its value using IEnumerator.But it does not return any value whereas the select query written inside fillDataTable() method returns data successfully.The last line is not returning any value.Please help.
My code is :-
DataTable dataTable = new DataTable();
dataTable = fillDataTable();//method used for filling datatable.
IEnumerator ienumerator = dataTable.Rows.GetEnumerator();
DataRow dataRow = (DataRow)ienumerator.Current;
string s = dataRow.ItemArray[0].ToString();

Thanks and Regards.
Posted
Updated 30-Jul-10 2:09am
v2

DataTable dataTable = new DataTable();
dataTable = fillDataTable();//method used for filling datatable.
IEnumerator ienumerator = dataTable.Rows.GetEnumerator();

//This line is missing
ienumerator.MoveNext();


DataRow dataRow = (DataRow)ienumerator.Current;
string s = dataRow.ItemArray[0].ToString();


Regards
Manuj Sharma
 
Share this answer
 
According to the MSDN documentation the default position of an enumerator is before the first element of the collection. Therefore you might have to call MoveNext() after creating the enumerator and before accessing the Current property...

However, calling Current before you move to the first element of the collection should throw an exception. As far as I understand your code does not throw an exception, so the problem might be somewhere else...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900