Solved

Index out of range datareader

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

705 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

21 Experts available now in Live!

Get 1:1 Help Now