Working with Data—Reading Rows of Data

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:Articles
Database Articles

Working with Data

© 2006 Jeffery Suddeth

Reading Rows of Data

The DataTable also has a property named Rows that stores the rows of data. The rows are actually objects of the DataRow class. The DataRow stores a collection of values that you can read or write to using an index. You can either use an integer—if you know the order of the columns—or you can use the name of the field. The following code segment loops through the table’s rows and obtains references to the Name, Phone, and Email fields.

foreach (DataRow row in ds.Tables[0].Rows) {
   // get the data
   string name = (string)row["Name"];
   string phone = (string)row["Phone"];
   string email = (string)row["Email"];
   }

The DataRow stores the values as object references so the data must be cast to the appropriate type before we can use it. In this case, we used strings.


Previous_Page_.gif Next_Page_.gif




Personal tools