Hello, I get this error: System.IndexOutOfRangeException: 2000 I've got a table this table in excel:

Month 2000 2001 2002 2003
1 180.4 194.3 209.0 236.4
2 181.1 195.2 212.2 237.9
3 182.4 196.3 213.1 238.7
4 184.1 197.4 216.5 240.1
5 185.4 198.6 219.4 240.2
6 186.1 199.0 220.7 240.7 (up to 2013)
7 187.7 200.1 224.5 241.1
8 189.2 200.5 227.0 241.0
9 189.8 201.0 228.2 240.8
10 190.6 201.9 230.5 240.8
11 191.6 203.0 231.9 240.5
12 192.2 203.9 232.8 240.4

private void Database()
{

string strConnString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|calculator2.mdb";

DateTime e59 = tenderDate;
DateTime e61 = e59.AddDays(14);
DateTime f61 = e61.AddDays(-15);
int h61 = f61.Month;
int i61 = f61.Year;

OleDbConnection con = new OleDbConnection(strConnString);
con.Open();
string strQuery2 = "select " + i61 + " from JBBC where Month=" + h61;
OleDbCommand cmd2 = new OleDbCommand(strQuery2, con);
OleDbDataReader reader2 = cmd2.ExecuteReader();
reader2.Read();
string yr2000 = reader2["2000"].ToString();
string yr2001 = reader2["2001"].ToString();
string yr2002 = reader2["2002"].ToString();
string yr2003 = reader2["2003"].ToString();
string yr2004 = reader2["2004"].ToString();
string yr2005 = reader2["2005"].ToString();
string yr2006 = reader2["2006"].ToString();
string yr2007 = reader2["2007"].ToString();
string yr2008 = reader2["2008"].ToString();
string yr2009 = reader2["2009"].ToString();
string yr2010 = reader2["2010"].ToString();
string yr2011 = reader2["2011"].ToString();
string yr2012 = reader2["2012"].ToString();
string yr2013 = reader2["2013"].ToString();

reader2.Close();
con.Close();
}

Recommended Answers

All 4 Replies

Have you hand-run your SQL statement to see what results it produces? It appears to me that the statement will only return a single column. However, in your statements after the "reader2.Read()", you are accessing multiple columns. If there's only one column in the result set, that code will definitely generate an IndexOutOfRange exception.

Hi, I've changed my code to this as the method is used a few times, but still no luck:

What needs to happen is if month=6 and year=2000 then "186.1" has to be returned

private void Database()
{
  // calculations
        DateTime today = DateTime.Now;
        DateTime e61 = tenderDate.AddDays(14);

        DateTime f16 = today.AddDays(-15);
        DateTime tenderMinus = tenderDate.AddDays(-15);
        DateTime f61 = e61.AddDays(-15);

        int h16 = f16.Month;
        int tenderMonth = tenderMinus.Month;
        int h61 = f61.Month;

        int i16 = f16.Year;
        int tenderYear = tenderMinus.Year;
        int i61 = f61.Year;

        double J59 = CalculateJBBC(tenderMonth, tenderYear);
        double J61 = CalculateJBBC(h61, i61);
        double J63 = CalculateJBBC(tenderMonth, tenderYear);
}

public double CalculateJBBC(int indexMonth, int indexYear)
    {
        double value = 0;
        string strConnString =
            "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|calculator2.mdb";

        OleDbConnection con = new OleDbConnection(strConnString);
        con.Open();

            // read from JBCC table
        string strQuery2 = "select " + indexYear + " from JBBC where Month=" + indexMonth;
        OleDbCommand cmd2 = new OleDbCommand(strQuery2, con);
        OleDbDataReader reader2 = cmd2.ExecuteReader();
        reader2.Read();

        string year = reader2[indexYear].ToString();

        reader2.Close();
        con.Close();

        value = Double.Parse(year);
        return value;
    }

One issue - the code:

string year = reader2[indexYear].ToString();

Is indexing the row using an integer value. For example, if "indexYear" is 2001, you will be trying to get the 2001st column of the row - of course, your row only has one column in it, hence the exception. Try this:

string year = reader2[indexYear.ToString()].ToString();

This will index the row by column name, rather than by number.

Thanks mcriscolo,

That makes alot of sense, silly me!

It works now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.