Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

DataTableReader is pretty much like SqlDataReader but it lets you iterate through the DataTables and DataSets.

Here is a simple code that demonstrate it. The important thing to note is that even if you add the row to the DataTable at the time of iteration the DataTableReader will pick that up.

 private void IteratingDataTable()
    {
        DataTable customerTable = GetDataTable();

        DataTableReader dtReader = 
new DataTableReader(customerTable);
        
        
while (dtReader.Read())
        {
            
            Response.Write( (
string) dtReader["Name"]);            
            Response.Write("<BR>");
            
// AddRow simply adds a ONLY ONE ROW and ends
            
AddRow(customerTable); 
        }
    }

On the other hand if you use the foreach loop to do the same thing you will get the error saying "Collection was modified":

Here is the forach loop but this throws the exception:

 private void UsingForEachLoop()
    {
        DataTable customerTable = GetDataTable();

        
foreach (DataRow row in customerTable.Rows)
        {
            Response.Write(row["Name"] 
as String);
            Response.Write("<BR>");
          
            
// Cannot add like this: Collection cannot be modified 
            
AddRow(customerTable);  
        }
    }

Apart from this basic issue there are lot more features that are added to the DataSet and DataTable controls in ASP.NET 2.0 which I will cover in my article soon.

powered by IMHO

 

Posted on Tuesday, December 13, 2005 10:20 PM | Back to top


Comments on this post: DataTableReader in Asp.net 2.0

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net