3

I am trying to create a SQL database in c# by having my program connect to the server. I am having a problem with creating a login screen. The problem is in order to create a login screen. I am trying to pass 2 strings to the connection class and am unable to do so since the statement is static?

public class sqlconnect
{
    public String us;
    public String pas;

    public SqlConnection myConnection = new SqlConnection("user id=" + (us) + ";" +
                                           "password=" + (pas) + ";server=PANDORA;" +
                                           "Trusted_Connection=yes;" +
                                           "database=NHS; " +
                                           "connection timeout=30");
}

ERROR - A field initializer cannot reference the non-static field, method, or property 

If I try and make "us" or "pas" static I am unable to pass information from my main class to this one. I would be grateful if someone could point out what I'm doing wrong as I am not an expert at C# I must admit. Thank you.

1
  • You need to run that code in a method after assigning username/password (most likely this is what you intended to do). EDIT: Boatload of other issues (username/password as clear text, input validation, public fields) but that's outside the question. Commented Oct 16, 2012 at 21:15

2 Answers 2

8

You can't use variables within a field initializer. A good option here would be to make myConnection a property or (better yet, in this case) a method:

public SqlConnection CreateConnection()
{
     return new SqlConnection("user id=" + us + ";" +
                                       "password=" + pas + ";server=PANDORA;" +
                                       "Trusted_Connection=yes;" +
                                       "database=NHS; " +
                                       "connection timeout=30");
}

This allows you to use your other variables (state) in order to create the value you want.

I would also recommend changing your string values to properties, having a constructor to initialize them property, and using better names. Another alternative would be to have the username and password be parameters to the method, ie:

public SqlConnection CreateConnection(string username, string password)
{
     return new SqlConnection("user id=" + username + ";" +
                                       "password=" + password + ";server=PANDORA;" +
                                       "Trusted_Connection=yes;" +
                                       "database=NHS; " +
                                       "connection timeout=30");
}

This would elimniate the need to store these within your class as state (unless they're used by other routines, as well).

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, I just noticed that, sorry!
@ChrisSinclair Just edited to include that suggestion, though ;) I think its another good option.
I did think about something like that however how would I then go about opening the connection from the main class? as my main class calls the method but it can't open the connection?
@Marshal that's likely another issue altogether. Are you doubly-sure that the information you're inputting and the method you're connecting are correct?
1
// property
public SqlConnection myConnection { get; set; }

// method
public SqlConnection GetSqlConnection()
{
     if (myConnection == null) 
       myConnection = new SqlConnection("user id=" + us + ";" +
                                       "password=" + pas + ";server=PANDORA;" +
                                       "Trusted_Connection=yes;" +
                                       "database=NHS; " +
                                       "connection timeout=30");

     return myConnection;
}

Comments

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.