Search code examples
vb.netdatasettableadapter

There is no row at position 0 error TableAdapter


I get this error every time I try to get the first column value of each row.

Dim modAdapter As New StudentSystemDBDataSetTableAdapters.modulesTableAdapter
Dim modDataset As New StudentSystemDBDataSet

If modAdapter.ModulesCountRow() <> 0 Then
    For x As Integer = 0 To modAdapter.ModulesCountRow()
        Dim column As String = modDataset.modules.Rows(x)(0).ToString
        MsgBox(column)
    Next
End If`

Solution

  • Indexes are zero based. So the first row is at modDataset.modules.Rows(0) and the last at modDataset.modules.Rows(modAdapter.ModulesCountRow() - 1).

    So you have to subtract 1:

    For x As Integer = 0 To modAdapter.ModulesCountRow() - 1
        Dim column As String = modDataset.modules.Rows(x)(0).ToString
        MsgBox(column)
    Next
    

    Edit: you can also use the DataTable.Rows.Count property if it's already filled. Then you don't need to get the row-count from database with the dataadapter:

    For x As Integer = 0 To modDataset.modules.Rows.Count - 1
        Dim column As String = modDataset.modules.Rows(x)(0).ToString
        MsgBox(column)
    Next