Solved

Data reader error

Posted on 2011-02-14
3
397 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

830 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