Solved

Index out of range datareader

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

15 Experts available now in Live!

Get 1:1 Help Now