System.IndexOutOfRangeException: 'indv_no'

Monorchism

New member
Joined
Jun 19, 2024
Messages
1
Programming Experience
Beginner
Hi, I have a simple issue and hope someone can help.
My code is
VB.NET:
Imports System.Data.OleDb

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim conn As New OleDbConnection
        conn.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\inetpub\private\mull.mdb")
        conn.Open()
        Dim strsql As String
        strsql = "select * from INDV"
        Dim cmd As New OleDbCommand(strsql, conn)
        Dim myreader As OleDbDataReader
        myreader = cmd.ExecuteReader
        myreader.Read()
        TextBox1.Text = myreader("indv_no")
    End Sub
End Class

I have checked that the database, table and field name exists.
When run I get the following message:
System.IndexOutOfRangeException: 'indv_no'
 
That indicates that there is no column with that name. You can check the column names like so:
VB.NET:
For i = 0 To myreader.FieldCount - 1
    Console.WriteLine(myreader.GetName(i))
Next
OT, INDV is a terrible table name. There's really no good reason to be using cryptic abbreviations for tables and the like these days. It's almost guaranteed that someone - possibly even you - is going to look at that name at some point and think "what the hell is that and why did some idiot name it that instead of something descriptive". If it's within your power, I strongly suggest that you change that name to something that actually describes what it is and do that for everything in future.
 
Back
Top