0

I am using Microsoft Practice Enterprice data to connect a WCF to database(s). Notice that this exception is not thrown always. It's happening inside the while statement. Then if I refresh the page (resubmit the login details, MVC app that communicate with this WCF service), the code execute without any problem. Can someone determine why the IDataRader is closed while the stack execution is in the using statement? My code is:

public class LoginService : ILoginService
{
    public Attendee CheckLoginCredentials(string username, string password, int databaseId)
    {
        Database db = DataBaseConnection.GetDatabaseObject(databaseId);
        Attendee attendee = new Attendee();
        try
        {
            DbCommand dbCommandWrapper = db.GetStoredProcCommand("Check_login_WCF");
            db.AddInParameter(dbCommandWrapper, "@Username", DbType.String, username);
            db.AddInParameter(dbCommandWrapper, "@Password", DbType.String, password);

            using (IDataReader dataReader = db.ExecuteReader(dbCommandWrapper))
            {
                while (dataReader.Read())
                {
                    if (!dataReader.IsDBNull(dataReader.GetOrdinal("PersonId")))
                        attendee.PersonId = dataReader.GetGuid(dataReader.GetOrdinal("PersonId"));


                    if (!dataReader.IsDBNull(dataReader.GetOrdinal("UserName")))
                        attendee.UserName = dataReader.GetString(dataReader.GetOrdinal("UserName"));


                    if (!dataReader.IsDBNull(dataReader.GetOrdinal("IsValid")))
                        attendee.ValidCredentials = dataReader.GetBoolean(dataReader.GetOrdinal("ValidCredentials"));
                }
            }


        }
        catch (Exception ex)
        {
            return null;
        }

        return attendee;
    }
}

DataBaseConnection:

public static Database GetDatabaseObject(int databaseId)
{
    string ConnectionString = string.Empty;
    ConnectionString = EventDatabases.GetConnectionString(databaseId);

    Database mydb = new Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase(ConnectionString);
    return mydb;
}
  • `catch (Exception ex) { return null; }` is a good way of NOT finding out what the error is. – H H Sep 15 '13 at 10:53
  • The `db` also deserves a `using(){}` block. You may suffer from to many open connections. – H H Sep 15 '13 at 10:55
  • Hi Henk: The exception is: "Invalid attempt to call MetaData when reader is closed." hence I put this title for the question. Thanks – Philip Flex Sep 15 '13 at 10:56
  • @HenkHolterman The db is not a disposable object, hance it can't be in using statement. – Philip Flex Sep 15 '13 at 11:01

0 Answers0