1

I am building an asp.net website in which I have to track different exceptions like duplicate user email or data inserted or not or foreign key exceptions but sql server 2016 always return error code -2146232060. I heard about duplicate error code is 2206 or something

This is my .DBManager class which I used to insert or retrieve data.

class DBManager : IDisposable
{
    private DbConnection con;
    private DbCommand command;

    public DBManager() : this("DefaultConnection") { }

    public DBManager(string connectionStringName)
    {
        string connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
        con = new SqlConnection(connectionString);
    }

    public int ExecuteNonQuery()
    {
        int result;

        if (command == null)
        {
            return -5;
        }

        try
        {
            con.Open();
            result = command.ExecuteNonQuery(); 
        }
        catch (DbException ex)
        {
            result = ex.ErrorCode;
        }
        finally
        {
            con.Close();
        }

        return result;
    }

    public DataTable ExecuteDataTable()
    {
        DataTable dt = new DataTable();
        SqlDataAdapter da;

        if (command == null)
        {
            return dt;
        }

        try
        {
            da = new SqlDataAdapter();
            con.Open();
            da.SelectCommand = (SqlCommand) command;
        }
        finally
        {
            con.Close();
        }

        da.Fill(dt);
        return dt;
    }

    public object ExecuteScalar()
    {
        object result;

        if (command == null)
        {
            return -5;
        }

        try
        {
            con.Open();
            result = command.ExecuteScalar();
        }
        catch (DbException ex)
        {
            result = ex.ErrorCode;
        }
        finally
        {
            con.Close();
        }

        return result;
    }

    public DataSet ExecuteDataSet()
    {
        DataSet ds = new DataSet();

        if (command == null)
        {
            return ds;
        }

        try
        {
            con.Open();
            DbDataAdapter ad = new SqlDataAdapter((SqlCommand)command);
            ad.Fill(ds);
        }
        finally
        {
            con.Close();
        }

        return ds;
    }

    public void SetSqlStringCommand(string commandText)
    {
        command = GetCommand(commandText, CommandType.Text);
    }

    public void SetStoredProcedureCommand(string commandText)
    {
        command = GetCommand(commandText, CommandType.StoredProcedure);
    }

    private DbCommand GetCommand(string commandText, CommandType commandType)
    {
        DbCommand command = con.CreateCommand();
        command.CommandType = commandType;
        command.CommandText = commandText;
        return command;
    }

    public void AddInParameter(string name, DbType dbType, object value)
    {
        if (command != null)
        {
            AddParameter(command, name, dbType, value, ParameterDirection.Input, 0);
        }

        else
        {
            throw new ArgumentException("Command cannot be null");
        }
    }

    public void AddOutParameter(string name, DbType dbType, int size)
    {
        if (command != null)
        {
            AddParameter(command, name, dbType, null, ParameterDirection.Output, size);
        }

        else
        {
            throw new ArgumentException("Command cannot be null");
        }
    }

    private void AddParameter(DbCommand command, string name, DbType dbType, object value, 
        ParameterDirection direction, int size)
    {
        var parameter = command.CreateParameter();
        parameter.ParameterName = name;
        parameter.DbType = dbType;
        parameter.Value = value ?? DBNull.Value;
        parameter.Direction = direction;

        if (size > 0)
        {
            parameter.Size = size;
        }

        command.Parameters.Add(parameter);
    }

    public void Dispose()
    {
        if (command != null)
        {
            command.Dispose();
            command = null;
        }

        if (con != null)
        {
            con.Dispose();
            con = null;
        }
    }
}

Is it because I am using DbConnection class instead of SqlConnection?

Hafiz Hamza
  • 311
  • 3
  • 16
  • You need to examine .Number of an SqlException for the server error code. (aside: adapters/tables require disposal also) – Alex K. Feb 06 '18 at 11:23
  • the error code you're getting is 0x80131904, and there's already a [question](https://stackoverflow.com/questions/26135346/sql-connection-error-system-data-sqlclient-sqlexception-0x80131904) related to it here on stackoverflow that might help you. – cosh Feb 06 '18 at 11:25
  • 1
    0x80131904 seems to be the HRESULT used for all SQL Server errors. You'll need to look at the other properties of the exception instance to see what kind of error you have,. – Richard Feb 06 '18 at 11:27
  • how can I check other properties of exception instance? – Hafiz Hamza Feb 06 '18 at 11:46
  • 1. Use a debugger – set a break point on the `catch`; 2. include at least the `Message` property in your diagnostics, 3. don't catch an exception without re-throwing unless you have effectively handled the exception. – Richard Feb 06 '18 at 13:10
  • Since this looks like a class designed to handle your database work you should not have ANY try/catch blocks. If the library encounters an error that error MUST get back to the calling program. You might also consider the USING statement as a much cleaner and safer way to handle your connections and other objects inheriting IDisposable. – Sean Lange Feb 06 '18 at 14:41

0 Answers0