0

I am trying to open a connection to a database and I have used a configuration string to connect and I've instantiated the sqlConnection object. I would like to be able to use this reference in a new classes method. Why am I not able to achieve this? Why does it say "this class cannot be inherited"?

namespace sqlLibrary
{
    public class sqlStuff
    {
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SurplusConnectionString"].ConnectionString);  

    }

    public class commands : sqlStuff
    {
        public void executeQuery()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = myConnection; --> Not able to get reference from sql stuff class? 
        } 
    }    
}
1
  • 2
    I doubt that's all it says. Show us the entire error message. All relevant info. Commented Jul 5, 2017 at 19:45

1 Answer 1

7

By default, fields (and other class members) have private access modifier. Thus they are not visible in child classes. You should explicitly specify protected visibility

 protected SqlConnection myConnection

See Accessibility Levels (C# Reference)

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

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.