Solved

IndexOutOfRangeException in one environment

Posted on 2012-03-28
1
236 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
1 Comment
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now