G
Guest
Hi everybody,
Is there anybody can tell me what this error meant? Where is source of
error? There is a class library DLL created by another person. I used C# (VS
2005 Professional Edition) and SQL Server 2005 Express Edition and also used
Visual SourceSafe 6.0.
In this class,
There is a constructor and destructor in the destructor it calls the
CloseConnection() method where the error occurs.
So Base on this codes, what is the problem?
Thanks in Advanced.
den2005
Is there anybody can tell me what this error meant? Where is source of
error? There is a class library DLL created by another person. I used C# (VS
2005 Professional Edition) and SQL Server 2005 Express Edition and also used
Visual SourceSafe 6.0.
In this class,
There is a constructor and destructor in the destructor it calls the
CloseConnection() method where the error occurs.
Code:
I am using this methods
public void CreateConnection(string ConnectionString) {
// Check first if the connection is open, if yes, close it first
before creating a new one
CloseConnection();
// Create the SQLConnection object for the class to use
m_Connection = new SqlConnection(ConnectionString);
}
public void CreateCommand(string StoredProcName) {
// Check first if the command is existing, if yes, set it first
to null before creating a new one
CloseCommand();
// Check if there is an SQLConnection object existing for the
command to use
if (m_Connection == null)
CreateConnection();
// Create the SQLCommand object for the class to use
m_Command = new SqlCommand();
m_Command.Connection = m_Connection;
m_Command.CommandType = CommandType.StoredProcedure;
m_Command.CommandText = StoredProcName;
}
public void AddParameter(string ParamName, object ParamValue) {
// Check if there is an SQLCommand existing before adding the
parameters. If not, then return an exception.
if (m_Command == null)
throw new NullReferenceException("Command Object is null.
Please execute CreateCommand first before adding parameters.");
// Add the parameter to the SQLCommand object
SqlParameter parameter;
parameter = m_Command.CreateParameter();
parameter.ParameterName = ParamName;
parameter.Value = ParamValue;
m_Command.Parameters.Add(parameter);
parameter = null;
}
public void ExecuteReader() {
// Check if there is an SQLCommand existing before executing
Data Reader. If not, then return an exception.
if (m_Command == null)
throw new NullReferenceException("Command Object is null.");
// Check if the connection state of the SQLConnection object is
open, if not open it.
if (m_Connection.State == ConnectionState.Closed)
m_Connection.Open();
// Close first and dispose the existing data reader is there is
any
CloseDataReader();
// Create the data reader
m_DataReader = m_Command.ExecuteReader();
}
Code:
~DataAccessLayer() {
Dispose(true);
}
private void CloseConnection()
{
if (m_Connection != null) {
try {
// Check if connection is already closed, if not, then
close it
if (m_Connection.State != ConnectionState.Closed)
m_Connection.Close(); <<---Error Occurs here
// Dispose the connection object and set it to null to
release resources used by the object
m_Connection.Dispose();
m_Connection = null;
}
catch {
throw;
}
}
}
private void CloseCommand() {
if (m_Command != null) {
try {
// Dispose the command object and set it to null to
release resources used by the object
m_Command.Dispose();
m_Command = null;
}
catch {
throw;
}
}
}
private void CloseDataReader() {
if (m_DataReader != null) {
try {
// Check if datareader is already closed, if not, then
close it
if (!m_DataReader.IsClosed)
m_DataReader.Close();
// Set datareader to null
m_DataReader = null;
}
catch {
throw;
}
}
}
private void CloseDataSet() {
if (m_DataSet != null) {
try {
// Dispose object to release resources used
m_DataSet.Dispose();
m_DataSet = null;
}
catch {
throw;
}
}
}
public void Dispose() {
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
// Check to see if Dispose has already been called.
if (!this.m_Disposed) {
if (disposing) {
// Dispose managed resources.
CloseDataReader();
CloseDataSet();
CloseCommand();
CloseConnection();
}
// Release unmanaged resources. If disposing is false,
// only the following code is executed.
}
m_Disposed = true;
}
So Base on this codes, what is the problem?
Thanks in Advanced.
den2005