Solved

Index out of range datareader

Posted on 2010-11-11
2
824 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 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