IndexOutOfRangeException in one environment

I'm getting an IndexOutOfRangeException in some of my C# code. The problem is that the code works perfectly in my development environment. On the Testing machine, I get the error. They of course work off of different databases, but they are the same thing, down to the data. Any idea what coudl cause it to work in one place but not the other. The offending code is below. I'm opeing as ASP.Net form that has two CheckBoxList controls and setting the checkboxes based on the values stored in the DB. The code is in the Page_Load event. Thanks for any help.

CheckBoxList Control = (CheckBoxList)Form1.FindControl("Control");

SqlConnection objConn = new SqlConnection("myconnection");
SqlCommand objCmd = new SqlCommand("select * from Field_Lookup", objConn);
Conrtol.DataSource = objCmd.ExecuteReader(CommandBehavior.CloseConnection);

objCmd = new SqlCommand("select * from vw_tierFields where RecID = " + Session["RecID"], objConn);
SqlDataReader objReader = objCmd.ExecuteReader();

while (objReader.Read())
ListItem currentCheckBox = Control.Items.FindByValue(objReader["Control"].ToString());
if (currentCheckBox != null)
currentCheckBox.Selected = true;
Who is Participating?
käµfm³d 👽Connect With a Mentor Commented:
The only place I see where such an exception could be raised is in this line:

ListItem currentCheckBox = Control.Items.FindByValue(objReader["Control"].ToString());

Open in new window way of indexing objReader with the string "Control". If the table you are selecting from doesn't provide that column, then it will not be in the resulting reader. Verify that you are selecting from the correct table, the correct database, and that the column you are interested in is indeed named "Control".
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.