12

I have one error in my code listed as: 'DatabaseLayer':type used in a using statement must be implicitly convertible to 'System.IDispsable'.

I tried looking at MSDN and other Stack Overflow questions, but I have not resolved the error. I am using Visual Studio 2015 with .NET Framework 4.5.1.

The error is in line 56, but I have commented above the line with error. Below is the code in my file:

public class UserDataAccessLayer : IDisposable
{
    public bool Create(string username, string pwd, string email)
    {
        bool retVal = false;
        SqlCommand sqlCmd = new SqlCommand("CreateUser");
        sqlCmd.CommandType = CommandType.StoredProcedure;
        sqlCmd.Parameters.AddWithValue("@Username", username);
        sqlCmd.Parameters.AddWithValue("@Pwd", pwd);
        sqlCmd.Parameters.AddWithValue("@Email", email);
        int result = new DatabaseLayer().ExecuteNonQuery(sqlCmd);
        if (result != Int32.MaxValue)
            retVal = true;

        return retVal;
    }

    public bool Create(string username, string pwd, string email, int roleId)
    {
        bool retVal = false;
        SqlCommand sqlCmd = new SqlCommand("CreateUser");
        sqlCmd.CommandType = CommandType.StoredProcedure;
        sqlCmd.Parameters.AddWithValue("@Username", username);
        sqlCmd.Parameters.AddWithValue("@Pwd", pwd);
        sqlCmd.Parameters.AddWithValue("@Email", email);
        int result = new DatabaseLayer().ExecuteNonQuery(sqlCmd);
        if (result != Int32.MaxValue)
            retVal = true;

        return retVal;
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    public User GetUserInfo(string username, string pwd)
    {
        User user = null;
        using (DatabaseLayer dbLayer = new DatabaseLayer())
        {
            SqlCommand sqlCmd = new SqlCommand("GetUserInfo");
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.Parameters.AddWithValue("@Username", username);
            sqlCmd.Parameters.AddWithValue("@Pwd", pwd);
            user = dbLayer.GetEntityList<User>(sqlCmd).FirstOrDefault();
        }
        return user;
    }
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240
Alex Martinez
  • 313
  • 1
  • 3
  • 9
  • 4
    DatabaseLayer doesn't implement IDisposable. So don't create it within a using statement. –  Feb 06 '17 at 21:16
  • 3
    While the error pretty clear, the code provided in the post is not - there is no information of what `DatabaseLayer` type is. Please read [MCVE] guidance on posting code. – Alexei Levenkov Feb 06 '17 at 21:19
  • As noted in https://stackoverflow.com/questions/21719567/implicitly-convertible-to-system-idisposable-error the warning can also appear if you are using Entity Framework but haven't installed the package. – Eborbob Sep 24 '19 at 14:19

2 Answers2

31

A using block:

using (DatabaseLayer dbLayer = new DatabaseLayer())
{
    //...
}

is essentially syntactic shorthand for this (or at least something reasonably close to it):

DatabaseLayer dbLayer = new DatabaseLayer();
try
{
    //...
}
finally
{
    dbLayer.Dispose();
}

Note that last line in the finally. If your object doesn't implement IDisposable then that can't compile. Hence the error.

Essentially, you have two options:

  1. Implement IDisposable on your type, or...
  2. Don't use a using block.
David
  • 208,112
  • 36
  • 198
  • 279
2

Followed David's advice to fix the error by taking off the "using() and the try and finally blocks. See corrected code below "public User GetUserInfo(...)":

public User GetUserInfo(string username, string pwd)
{
    User user = null;
    DatabaseLayer dbLayer = new DatabaseLayer();

    try
    {
        SqlCommand sqlCmd = new SqlCommand("GetUserInfo");
        sqlCmd.CommandType = CommandType.StoredProcedure;
        sqlCmd.Parameters.AddWithValue("@Username", username);
        sqlCmd.Parameters.AddWithValue("@Pwd", pwd);
        user = dbLayer.GetEntityList<User>(sqlCmd).FirstOrDefault();
    }

    finally
    {
        dbLayer.Dispose();
    }
    return user;
}
Alex Martinez
  • 313
  • 1
  • 3
  • 9