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;
}
}
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:
IDisposable
on your type, or...using
block.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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With