Solved

IndexOutOfRangeException in one environment

Posted on 2012-03-28
1
237 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 75

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

815 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

13 Experts available now in Live!

Get 1:1 Help Now