How do I read records from the data tables?
You can read the records from the data table using its Rows collection. With the Rows collection, you need to specify the row number and column name or number to access a particular field of the specified row. For example, if we have read the ‘Student’ table in our data table, we can access its individual fields as:
C# Version
DataTable dt = ds.Tables["student"];
string stId = dt.Rows[0]["StudentID"].ToString();
string stName = dt.Rows[0]["StudentName"].ToString();
string stDateOfBirth = dt.Rows[0][2].ToString();
VB.Net Version
Dim dt As DataTable
dt = ds.Tables("student")
Dim stId As String
stId = dt.Rows(0)("StudentID").ToString()
Dim stName As String
stName = dt.Rows(0)("StudentName").ToString()
Dim stDateOfBirth As String
stDateOfBirth = dt.Rows(0)(2).ToString()
Here we have retrieved various fields of the first record of the student table read in the dataset. As you can see, we can either specify the column name in string format or we can specify the column number in integer format. Also note that the field value is returned in the form of Object, so we need to convert it to the string before using it. Similarly, you need to cast variables of other data types before using them.
C# Version
int stAge = int.Parse(dt.Rows(0)("Age").ToString());
VB.Net Version
Dim stage As Integer
stAge = Integer.Parse(dt.Rows(0)("Age").ToString())
Back