vb.netdatatabledatatable.select

How to select data from datatable


I am little bit stuck here .. I have a datatable as _

Dim dtPupil As New DataTable

dtPupil.Columns.Add("PupilId", GetType(Integer))
dtPupil.Columns.Add("Forename", GetType(String))
dtPupil.Columns.Add("Surname", GetType(String))

I made a select (assuming names combination will be unique)

Dim strQuery As String = "Forename ='" & forename & "' and Surname = '" & surname & "'"
Dim dr As DataRow()
dr = dTablePupil.Select(strQuery)

I wanna have PupilId as an Integer of the row that match, so

Dim PupilID As Integer = ?????????? 

What do I need to write here? There will only be 1 row returned.


Solution

  • Firstly check the length of DataRow array,

    IF dr.Length<>0 Then
      PupilID= CType(dr(0)("PupilId"),Integer)
    End If