0

I am trying to add multiple objects to a list (tmpUsers) but when I try to add the second object I get into the catch block? Can someone see if I am doing it the right way and help me understand where I am going wrong?

public List<User> GetFilteredUsers(long CompanyKey, int sitekey, int filtertype=4, string filtervalue="", UserRequestContext requestContext = null)
{
    List<User> tmpUsers = new List<User>();

    Database db = null;
    IDataReader dr = null;
    DbCommand dbCommand = null;

    try
    {
        db = DatabaseFactory.CreateDatabase("vaultDataConfiguration");
        dbCommand = db.GetStoredProcCommand("GetFilteredUsers");
        db.AddInParameter(dbCommand, "@CompanyKey", DbType.Int32, CompanyKey);
        db.AddInParameter(dbCommand, "@Sitekey", DbType.Int32, sitekey);
        db.AddInParameter(dbCommand, "@FilterType", DbType.Int32, filtertype);
        db.AddInParameter(dbCommand, "@FiterValue", DbType.String, filtervalue);
        dr = db.ExecuteReader(dbCommand);
        User tmpUser = new User();
        while (dr.Read())
        {
            
            tmpUser.GroupName = dr["groupName"] != DBNull.Value ? dr["groupName"].ToString() : "";
            tmpUsers.Add(tmpUser);
        }
        tmpUser = new User();
        tmpUser.CompanyUdidDescription = dr["companyUDIDDescription"] != DBNull.Value ? dr["companyUDIDDescription"].ToString() : "";
        tmpUsers.Add(tmpUser);
        return tmpUsers;
    }
    catch (Exception ex)
    {
        LogWriter.WriteAuditLog(ex.Message, LogLevel.ERROR, exception: ex, _event: "UserDAO->GetFilteredUserDetails", requestContext: requestContext);
        throw;
    }
    finally
    {
        if (dr != null)
        {
            dr.Close();
            dr.Dispose();
        }
    }
}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
rock rake
  • 5
  • 1
  • what is the exception? – Jonathan Jun 23 '21 at 15:28
  • Add user is happening out of While loop. Not sure how / when you get to adding second user – Ankit Jun 23 '21 at 15:30
  • You presumably want to create a new `User` object for each record in the database, set its `GroupName` and `CompanyUdidDescription` properties from the database, and add the `User` object to the list, right? You really should move the line `User tmpUser = new User();;` inside the `while` loop, along with the code after the `while` loop, before the `return`. – Heretic Monkey Jun 23 '21 at 15:42

2 Answers2

0

You are trying to read after the readers recors are over. Try this code

 
 while (dr.Read())
{
  var tmpUser = new User();                   
  tmpUser.GroupName = dr["groupName"] != DBNull.Value ? dr["groupName"].ToString() : "";
 tmpUser.CompanyUdidDescription = dr["companyUDIDDescription"] != DBNull.Value ? 
 dr["companyUDIDDescription"].ToString() : "";
 tmpUsers.Add(tmpUser);
}
return tmpUsers;
Serge
  • 40,935
  • 4
  • 18
  • 45
0

When you try to assign tmpUser.CompanyUdidDescription, you've already gotten to the end of your DataReader, but you are still trying to read from it.

You can't do that.

See: DataReader cursor rewind

Jonathan
  • 4,916
  • 2
  • 20
  • 37