Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Data reader error

Posted on 2011-02-14
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 17

Accepted Solution

Daniel Reynolds earned 1000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

730 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