Search code examples
c#asp.netidatareader

Could not read values using IDataReader


I want to read data to a list from database.

I tried the following code

public List<T> StoredProcedureForIList<T>(string spName, params IDataParameter[] commandParameters)
{
   List<T> list = new List<T>();

   T item;
   Type listItemType = typeof(T);
   item = (T)Activator.CreateInstance(listItemType);
   list.Add(item);

   using (IDatabaseConnection connection = new DatabaseConnection())
   {
       IDbCommand cmd = connection.CreateCommandForStoredProcedure(spName);
       foreach (SqlParameter par in commandParameters)
       {
          cmd.Parameters.Add(par);
       }

       try
       {
          using (IDataReader reader = cmd.ExecuteReader())
          {
             while (reader != null && reader.Read())
             {
                for (int i = 0; i < reader.FieldCount; i++)
                {
                   var prop = listItemType.GetProperty(reader.GetName(i));
                   prop.SetValue(item, reader[i], null);
                }
                list.Add(item);
             }
          }
       }
       catch(Exception ex)
       { }

       return list;
   }
} 

But the problem is that when the for loop starts the reader loses data.

The data reader ResultView value is Enumeration yielded no results.


Solution

  • My guess is that some error occurs during execution of your loop. This technique...

    try 
    { 
        ...
    } 
    catch(Exception ex) 
    { } 
    

    ...ensures that this error is ignored and all you get is an incomplete result. As you have noticed, this makes debugging quite hard. So don't do that.

    Thus, the solution is:

    • remove the try-catch block (i.e., replace try { ... } catch(Exception ex) {} by ...),
    • run the code again,
    • note the error that occurs,
    • if you understand the error
      • fix it
    • else
      • ask again on StackOverflow, in a new question.

    And, never, never write catch (Exception ex) {} again. ;-) Do proper error handling, or don't do error handling at all.