?
Solved

IndexOutOfRangeException in one environment

Posted on 2012-03-28
1
Medium Priority
?
246 Views
Last Modified: 2012-04-01
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();
0
Comment
Question by:dodgerfan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 37779666
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


...by 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".
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question