aaron b
aaron b

Reputation: 31

Unhandled exception occurred - System.IndexOutOfRangeException: upc

I added a new table to our database named upc.

When I add it to the front end of our site, I'm getting this error:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: upc

Source Error:

    if (!Sdr["upc"].ToString().Trim().Equals(""))

           liupc.InnerHtml = "upc: " + Sdr["upc"].ToString();

    else

I'm not sure what this error means or how I can resolve it - any insight is much appreciated.

Upvotes: 0

Views: 3615

Answers (3)

seairth
seairth

Reputation: 2062

I'm guessing (you really need to show more code) that "Sdr" is a SqlDataReader instance. If that's the case, you would not reference the table inside the indexer, but columns in the table (or whatever resultset) after a call to SqlCommand.ExecuteReader().

Something like:

SqlCommand cmd = ...;

SqlDataReader Sdr = cmd.ExecuteReader();

while(Sdr.Read())
{
    var value = Sdr["column_name"];
    // etc...
}

Also, see System.Data.SqlClient.SqlDataReader.

Upvotes: 2

Chuck Savage
Chuck Savage

Reputation: 11955

change line 120 to be,

if(!string.IsNullOrEmpty(Sdr["upc"]))

and try that.

Upvotes: 0

Nick Rolando
Nick Rolando

Reputation: 26167

Simply put, it isn't recognizing "upc" as a string index in your array Sdr.

Upvotes: 1

Related Questions