?
Solved

Index out of range datareader

Posted on 2010-11-11
2
Medium Priority
?
829 Views
Last Modified: 2012-08-13
I am having a problem with my function.  I have lots of similar functions that work fine but cannot seem to find what is wrong with the following.  I wondered if anyone looking at it fresh might spot the problem.

I get the error at
 ExtensionUser = (string)UserDataReader[ExtensionUser].ToString();

the fields ExtensionUser and ExtensionNo ar both varchars in a table called extensions.

If i set a breakpoint and copy the sqltext string vlaue and paste it into sql query analyser it returns the value i expect.

Many Thanks
public static string GetExtensionUserFromExt(string ExtensionNo)
        {


            string ExtensionUser = "";

            // declare datareader, connection and command name
            SqlDataReader UserDataReader;
            SqlConnection UserDataConnection;
            SqlCommand UserDataCommand;

            // start new command with command text 
            UserDataConnection = new SqlConnection(ConnString());

            // open connection
            UserDataConnection.Open();

            // declare sql text
            string sqltext = "select [ExtensionUser] from extensions where [ExtensionNo] = '" + ExtensionNo + "'";

            //prepare sql statements
            UserDataCommand = new SqlCommand(sqltext, UserDataConnection);
            UserDataReader = UserDataCommand.ExecuteReader();


            while (UserDataReader.Read())
            {

                ExtensionUser = (string)UserDataReader[ExtensionUser].ToString();
                
            }




            //cleanup objects
            UserDataReader.Close();
            UserDataConnection.Close();


            return ExtensionUser;



        }

Open in new window

0
Comment
Question by:Ross-C
[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
2 Comments
 
LVL 3

Accepted Solution

by:
xeonol earned 2000 total points
ID: 34109953
This line:
ExtensionUser = (string)UserDataReader[ExtensionUser].ToString();

should be updated to:
ExtensionUser = (string)UserDataReader["ExtensionUser"].ToString();

or it actually input a wrong/empty index value as you defined at the beginning in this function:
string ExtensionUser = "";

0
 
LVL 11

Author Closing Comment

by:Ross-C
ID: 34109999
How did I not spot that, had been looking at it too long. Many thanks for that.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
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…

719 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