Reader ordinal problem

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I am getting the System.IndexOutOfRangeException was unhandled error on the
last line of below code;

Cmd = New OleDb.OleDbCommand("SELECT [Last Staff Update From Site] FROM
[Update Control]", LocalConn)
Reader = Cmd.ExecuteReader()
Console.WriteLine("{0}", Reader.GetOrdinal("[Last Staff Update From Site]"))

What is the problem and how can I fix it?

Thanks

Regards
 
A DataReader is initialized so that it is pointing just BEFORE the first
record returned by your query. As such, you aren't pointing to any data
when you attempt to get the ordinal in question.

You must advance the reader to the first record before querying it:

Cmd = New OleDb.OleDbCommand("SELECT [Last Staff Update From Site] FROM
[Update Control]", LocalConn)
Reader = Cmd.ExecuteReader()

Do While Reader.Read()
Console.WriteLine("{0}", Reader.GetOrdinal("[Last Staff Update From
Site]"))
Loop
 
John,
Have you tried:
Console.WriteLine("{0}", Reader.GetOrdinal("Last Staff Update From Site"))

Generally this is why I avoid including spaces in field names...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


John said:
Hi

I am getting the System.IndexOutOfRangeException was unhandled error on
the last line of below code;

Cmd = New OleDb.OleDbCommand("SELECT [Last Staff Update From Site] FROM
[Update Control]", LocalConn)
Reader = Cmd.ExecuteReader()
Console.WriteLine("{0}", Reader.GetOrdinal("[Last Staff Update From
Site]"))

What is the problem and how can I fix it?

Thanks

Regards
 
Jay B. Harlow said:
John,
Have you tried:


Generally this is why I avoid including spaces in field names...

Oh yes, having columns with blanks is really a bad idea.
 
Back
Top