Hi Everybody,
I am new to C# and am trying to develop a component for database access. I am writing a class object where I create new connection and command objects in the constructor and try to use those declared objects in different methods in the class. It goes something like this:
namespace <namespace>
{
public class DataObj
{
private SqlConnection oConn;
private SqlCommand oCmd;
public DataObj
{
SqlConnection oConn = new SqlConnection();
SqlCommand oCmd = new SqlCommand();
}
public void OpenConnection()
{
oConn.ConnectionString = <Connection str>;
oConn.Open();
oCmd.Connection = oConn;
}
}
}
I get an error in the OpenConnection method when trying to use oConn.Open. The error states that the object has not been instantiated.
Appreciate it
Antzz
I am new to C# and am trying to develop a component for database access. I am writing a class object where I create new connection and command objects in the constructor and try to use those declared objects in different methods in the class. It goes something like this:
namespace <namespace>
{
public class DataObj
{
private SqlConnection oConn;
private SqlCommand oCmd;
public DataObj
{
SqlConnection oConn = new SqlConnection();
SqlCommand oCmd = new SqlCommand();
}
public void OpenConnection()
{
oConn.ConnectionString = <Connection str>;
oConn.Open();
oCmd.Connection = oConn;
}
}
}
I get an error in the OpenConnection method when trying to use oConn.Open. The error states that the object has not been instantiated.
Appreciate it
Antzz