Um...I think you need a bit of practice at this...
You create a new instance of the class:
new SQLConnect();
But you don't keep a reference to it: that's like buying a packet of sweets in a shop and leaving them behind when you leave: when you get home, you can't eat any of the sweeties because you no longer have the instance you bought.
So start by keeping the reference:
SqlConnect con = new SQLConnect();
But...you don't keep the SqlConnection you create within your class either! So when you exit the constructor, once again you can't get at the sweeties! :laugh:
So add a Property to allow you to retrieve it:
public class SQLConnect
{
private string myConnection;
public SqlConnection Connection { get; private set; }
public SQLConnect()
{
myConnection = ConfigurationManager.ConnectionStrings["EQCas"].ConnectionString;
Connection = new SqlConnection(myConnection);
Connection.Open();
}
}
Now, the connection will last as long as the reference to your class does.
Now, you need to "attach" the Command to the Connection or the SqlCommand doesn't know where it is supposed to execute:
SqlCommand myCommand = new SqlCommand("select x_id, fullname, email FROM cas_user_ext WHERE fullname = @fullname", con.Connection);
This isn't an optimal solution: It doesn't allow for closing the connection (which you need to do) or Disposing of it (which you also need to do)
I'd probably do it differently: have a static method (or property) in my class which returned the connection string, and let the external code create, open, use and Dispose the SqlConnection object directly.
[edit]Typos - OriginalGriff[/edit]