Link to home
Start Free TrialLog in
Avatar of ayha1999
ayha1999

asked on

Data reader error

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
ASKER CERTIFIED SOLUTION
Avatar of Daniel Reynolds
Daniel Reynolds
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
By the way what kind of Construct is maxReader["newId "]???
Is it an ArrayList object that stores Integer Values?? Or something else.. Please clear my doubts first..