Data reader error

Posted on 2011-02-14
Last Modified: 2012-05-11
when I try to execute the following code I get an error; the sql command isworking fine.

System.IndexOutOfRangeException: maxNumber

at line
newId = maxReader["maxNumber"].ToString();

      SqlCommand cmd = new SqlCommand("SELECT MAX(CAST(SUBSTRING(wid, 4, len(wid)) as INT))+1 as newId from myTable", cn);
        SqlDataReader Reader;
int newid;

if (!blnMatch)
                    foreach (DataRow objDR in objDT.Rows)
         Reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                  while (Reader.Read())
                      newId = maxReader["newId "].ToString();
                                                cmdPart.Parameters["@newid"].Value = newId;
                                                cmdPart.Parameters["@userid"].Value = User.Identity.Name;
                                                cmdPart.Parameters["@date"].Value = DateTime.Now;

pls help.

Question by:ayha1999
  • 2
LVL 17

Accepted Solution

xDJR1875 earned 250 total points
ID: 34890451
at first glance I wonder if the object you are trying to iterate has any rows in it. ( foreach (DataRow objDR in objDT.Rows))

If the query is working, I would check the objDT object for rows and go from there.
LVL 12

Expert Comment

ID: 34891077
By the way what kind of Construct is maxReader["newId "]???
LVL 12

Expert Comment

ID: 34891092
Is it an ArrayList object that stores Integer Values?? Or something else.. Please clear my doubts first..

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at

708 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

13 Experts available now in Live!

Get 1:1 Help Now