Solved

Data reader error

Posted on 2011-02-14
3
400 Views
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)
                    {
                                cn.Open();                                        
                    foreach (DataRow objDR in objDT.Rows)
                                            {
         Reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                  while (Reader.Read())
                      {
                      newId = maxReader["newId "].ToString();
                       }
                                                  Reader.Close();
                           
                                                cmdPart.Parameters["@newid"].Value = newId;
                                                cmdPart.Parameters["@userid"].Value = User.Identity.Name;
                                                cmdPart.Parameters["@date"].Value = DateTime.Now;
   
                                                try
                                                {
                                                    cn.Open();
                                                    cmd2.ExecuteNonQuery();
                                                 }
                                                 catch
{
}

pls help.

ayha
0
Comment
Question by:ayha1999
[X]
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
3 Comments
 
LVL 17

Accepted Solution

by:
Daniel Reynolds 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.
0
 
LVL 12

Expert Comment

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

Expert Comment

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

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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