what is wrong with this class?

public class DBInterface
{
private OleDbConnection connection;

public DBInterface(string strcon)
{
connection = new OleDbConnection(strcon);
}
public DBInterface()
{
this.connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\testtab.mdb");

if (true)
{
this.connection.Open();

if (this.connection != null)
Console.Write("Connection State: " + this.connection.State);
else
Console.Write("Connection State: Not Established ");
}

}

~DBInterface()
{
// Destructor logic
}

public void connect()
{
this.connection.Open();
}

public void disconnect()
{
this.connection.Close();
}

public bool getState()
{

if (this.connection != null)
Console.Write("Connection State: " + this.connection.State);
else
Console.Write("Connection State: Not Established Yet");

if (this.connection != null)
{
if (connection.State.Equals("Open"))
return true;
}
return false;
}

the BOLD part says connected , but once you set the true to false and do it this way:
it says it is not connected.
there must be something wrong with the creation of the object 'connection'

DBInterface db = new DBInterface();
// Connnect to database
db.connect();
string str;
if (db.getState())
Console.WriteLine("db is connected");
else
Console.WriteLine("db is not connected");
[2279 byte] By [Saeed] at [2007-11-20 10:40:38]
# 1 Re: what is wrong with this class?
The problem is this:if (connection.State.Equals("Open"))The OleDbConnection.State property is type ConnectionState, so it can NEVER be equal to the string "Open" no matter what state the connection is in. That means that your getState method ALWAYS returns false no matter what. That line of code should be:if (connection.State == ConnectionState.Open)Absolutely ALWAYS compare like with like. NEVER compare anything to a string if it isn't already a string itself. Strings are not some multipurpose type that can be used in any situation. They are a specific type of object and they are different to every other type.
jmcilhinney at 2007-11-9 11:35:45 >
# 2 Re: what is wrong with this class?
Good stuff mate ;)
cheers

You must spread some Reputation around before giving it to jmcilhinney again.
Saeed at 2007-11-9 11:36:46 >