0

I am developing a series of processes in C# which reuse the same SQL code. Below is an example of a Class I created for the SQL Connection to my test database. Question: how to do I call the Class in my process? I have tried a couple of things however, I get the below errors

Errors:

SQLHelperCode.FirstConnect is a 'type'  which is not valid in the given context.
Only Assignment, call, increment, decrement and new object expressions can be used as a statement

Class FirstConnect

public class FirstConnect
{
    public FirstConnect()
    {
        SqlConnection conn;
        string path = @"C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\Data";
        const string dbName = "datadetail";
        {
            conn = new SqlConnection("user id=TestUser;" +
                                     "server=TestData\\SQLEXPRESS;" +
                                     "Trusted_Connection=yes;" +
                                     "connection timeout=30");

            try
            {
                conn.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}

Want to call the FirstConnect in this code:

protected override void OnBarUpdate()
{
    accountName = Account.Name.ToString();

    if (accountName == 'Test1234')
    {
        //Call FirstConnect here.
    }
}
2
  • Instantiate the class and invoke the method like so: FirstConnect myThing = new FirstConnect(); myThing.DoSomething. The code you have provided above does not have a method on the class to call. Commented May 25, 2012 at 17:00
  • replace //Call FirstConnect here. with new FirstConnect(); And make sure you close your connection Commented May 25, 2012 at 19:12

3 Answers 3

5

This line defines the class

public class FirstConnect
 {

This line defines the constuctor

    public FirstConnect()
      {

The following will define a variable of type FirstConnect and then call the constructor to create it (I made it two lines to be explicit)

FirstConnect fc;
fc  = new FirstConnect();

Typically you'd then want to have method that actually does something with the object

e.g.

SomeOtherObject varaibleName = fc.GetSomeData(accountName);
Sign up to request clarification or add additional context in comments.

Comments

1

FirstConnect fc = new FirstConnect();

Comments

1

Not an answer but just a large comment...

When using classes that implement IDisposable like SqlConnection does, they should be used like so:

using (var conn = new SqlConnection("user id=TestUser;server=TestData\\SQLEXPRESS;Trusted_Connection=yes;connection timeout=30"))
{
   //... do work ... 
}

1 Comment

What you wrote is true but doesn't apply here. conn is a field on FirstConnect so the scope of conn can't be constrained to a using block. So FirstConnect should either implement IDisposable or be refactored so that it has connectionString field that gets used as you suggest in each method that uses it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.