Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type used in a using statement must be implicitly convertible to 'System.IDisposable'

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;
    }
}
like image 427
Alex Martinez Avatar asked Feb 06 '17 21:02

Alex Martinez


2 Answers

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.
like image 149
David Avatar answered Nov 05 '22 10:11

David


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;
}
like image 2
Alex Martinez Avatar answered Nov 05 '22 10:11

Alex Martinez