Solved

Index out of range datareader

Posted on 2010-11-11
2
823 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
2 Comments
 
LVL 3

Accepted Solution

by:
xeonol earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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