Link to home
Start Free TrialLog in
Avatar of dodgerfan
dodgerfanFlag for United States of America

asked on

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);
objConn.Open();
Conrtol.DataSource = objCmd.ExecuteReader(CommandBehavior.CloseConnection);
Control.DataBind();

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

while (objReader.Read())
{
ListItem currentCheckBox = Control.Items.FindByValue(objReader["Control"].ToString());
if (currentCheckBox != null)
{
currentCheckBox.Selected = true;
}
objReader.Close();
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial