0

Error:

System.IndexOutOfRangeException at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName) at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name) at System.Data.SqlClient.SqlDataReader.get_Item(String name) at

I alter my stored procedure to add two new columns to my result, the stored procedure by itself seems to be working but when i add the new columns to my SQlDataReader im getting that error

Model

public class StudentRecordFull : StudentRecord
{
    public double CreditProgress { get; set; }
    public string EnrollmentStatus { get; set; }
    public int IsEnrolled { get; set; }
    public string YRQLastAttended { get; set; }
    public int Mandatory { get; set; }
    public double GPA { get; set; }
    public string TotalPrint { get; set; }
    public int Balance { get; set; }

}

my Method

public StudentRecordFull GetStudent(int sid)
    {
        StudentRecordFull s = new StudentRecordFull();
        using (SqlConnection myConnection = new SqlConnection(ProductionDataConnString))
        {
            SqlCommand myCommand = new SqlCommand("usp_GetStudent", myConnection);
            myCommand.CommandType = CommandType.StoredProcedure;
            SqlParameter SIDParameter = myCommand.Parameters.Add("SID", SqlDbType.Char, 9);
            SIDParameter.Value = sid;

            myConnection.Open();
            using (SqlDataReader oReader = myCommand.ExecuteReader())
            {
                while (oReader.Read())
                {
                    s.SID = SafeGetString(oReader, "SID");
                    s.FirstName = SafeGetString(oReader, "FirstName");
                    s.LastName = SafeGetString(oReader, "LastName");
                    s.AdvisorName = SafeGetString(oReader, "Advisor");
                    s.AdvisorSID = SafeGetString(oReader, "AdvisorSID");
                    s.PathwayName = SafeGetString(oReader, "PathwayName");
                    s.PathwayID = SafeGetInt(oReader, "PathwayId");
                    s.SubPathwayID = SafeGetInt(oReader, "SubPathwayId");
                    s.SubPathwayName = SafeGetString(oReader, 
                                                      "SubPathwayName");
                    //s.TotalPrint = SafeGetString(oReader, "ploplo");
                    s.Credits = SafeGetInt(oReader, "CreditsEarned");
                    s.Phone = SafeGetString(oReader, "DaytimePhone");
                    s.DOB = SafeGetString(oReader, "DOB");
                    s.Address = SafeGetString(oReader, "Address");
                    s.City = SafeGetString(oReader, "City");
                    s.State = SafeGetString(oReader, "State");
                    s.Zip = SafeGetString(oReader, "Zip");
                    s.Email = SafeGetString(oReader, "Email");
                    s.CreditProgress = SafeGetDouble(oReader, 
                                           "CreditProgress");
                    s.EnrollmentStatus = SafeGetString(oReader, 
                                           "EnrollmentStatus");
                    s.IsEnrolled = SafeGetInt(oReader, "IsEnrolled");
                    s.YRQLastAttended = SafeGetString(oReader, 
                                            "YRQLastAttended");
                    s.Mandatory = SafeGetInt(oReader, "Mandatory");
                    s.GPA = SafeGetDouble(oReader, "GPA");
                    s.Balance = SafeGetInt(oReader, "Balance");
                    //s.TotalPrint = SafeGetString(oReader, "TotalPrint");

                }
                myConnection.Close();
            }
        }
        return s;
    }

Helper

private int SafeGetInt(SqlDataReader reader, string colName)
    {
        int i = 0;
        if (!DBNull.Value.Equals(reader[colName]))
        {
            int.TryParse(reader[colName].ToString(), out i);
        }
        return i;
    }

If i remove the new columns everything works great is like it cant not read those columns .

Any idea?

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
user3376642
  • 535
  • 2
  • 8
  • 18
  • 1
    Clearly your sp doesn't return the two new columns. Are you sure that you are working on the correct database? Check the connectionstring. Could you show it. – Steve Jun 22 '18 at 15:35
  • Double check your database connection. Since changes are not made on PROD until they have been tested, I'd infer from your variable name that you're reading from that PROD database instead of your test database. Also, please include the full error stack trace, as it could have come from `SafeGetString` – Elaskanator Jun 22 '18 at 15:35
  • I think you might be presuming that your `SafeGetInt` will work even if the column is not returned. Your not checking is the column exists, only that it may or may not be null. – Liam Jun 22 '18 at 15:36

0 Answers0