Why am I getting "is inaccessible due to its protection leve

  • Thread starter SlurrerOfSpeech
  • Start date
  • Tags
    Protection
In summary, the conversation discusses an error in Visual Studio related to creating a DbSet<T> instance. Possible solutions are checking Windows rights in the user directory and ensuring a proper database sign-on. It is also mentioned that the constructor of DbSet<T> is protected and should be created from the context object.
  • #1
SlurrerOfSpeech
141
11
Visual Studio is giving me the error

Error 1 'System.Data.Entity.DbSet<Survey.Models.Survey>.DbSet()' is inaccessible due to its protection level c:\users\jamin\documents\visual studio 2013\Projects\Survey\Survey\Models\SurveyDbModel.cs 83 40 Survey

on the line

Code:
DbSet<Survey> AllSurveys = new DbSet<Survey>();

of

Code:
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Survey.Models
{

    public class Survey
    {
        public int Id { get; set; }
        [StringLength(100)]
        public string Title { get; set; }
    }

    public class Parnter
    {
        public int Id { get; set;}
        [StringLength(50)]
        public string Name { get; set; }
    }

    public class Question
    {
        public int Id { get; set; }
        public int SurveyId { get; set; }
        [StringLength(300)]
        public string QText { get; set; }
    }

    public class Answer
    {
        public int Id { get; set; }
        public int QuestionId { get; set; }
        public int PartnetId { get; set; }
        public decimal Val { get; set; }
    }

    public class SurveyDbModel
    {
        // name of connection string for database that
        private static readonly string _ConnStrName = "LocalDb";

        private SqlConnection _Conn;

        public SurveyDbModel ( )
        {
            this._Conn = new SqlConnection(ConfigurationManager.ConnectionStrings[SurveyDbModel._ConnStrName].ConnectionString);
        }

        ~SurveyDbModel ( )
        {
            this._Conn.Dispose();
        }

        public void AddSurvey ( string survtitle )
        {
            SqlCommand cmd = new SqlCommand("AddSurvey", this._Conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@title", survtitle);
            this._Conn.Open();
            cmd.ExecuteNonQuery();
            this._Conn.Close();
        }

        public void DeleteSurvey ( int survid )
        {
            SqlCommand cmd = new SqlCommand("DeleteSurvey", this._Conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id", survid);
            this._Conn.Open();
            cmd.ExecuteNonQuery();
            this._Conn.Close();
        }

        public DbSet<Survey> GetAllSurveys ( )
        {
            SqlCommand cmd = new SqlCommand("GetAllSurveys", this._Conn);
            cmd.CommandType = CommandType.StoredProcedure;
            this._Conn.Open();
            DbSet<Survey> AllSurveys = new DbSet<Survey>();
            using ( SqlDataReader dataReader = cmd.ExecuteReader() )
            {
                while ( dataReader.Read() )
                {
                    Survey srv = new Survey
                    {
                        Id = int.Parse(dataReader[0].ToString()),
                        Title = dataReader[1].ToString()
                    };
                    AllSurveys.Add(srv);
                }
            }
            this._Conn.Close();
            return AllSurveys;
        }

        public DbSet<Question> GetQuestionsBySurveyId ( int survid )
        {
            SqlCommand cmd = new SqlCommand("GetQuestionsBySurveyId", this._Conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id", survid);
            this._Conn.Open();
            DbSet<Question> TheseQuestions = (DbSet<Question>)cmd.ExecuteScalar();
            this._Conn.Close();
            return TheseQuestions;
        }

    }
}

Any idea why? Personally, I can't see how any access modifiers are getting in the way of me creating a DbSet<T> where T is a Survey.
 
Technology news on Phys.org
  • #2
Looks like an access problem on the OS level. Check you're Windows rights in the folder.
 
  • #3
SlurrerOfSpeech said:
c:\users\jamin
Just checking the obvious - that is your user directory, correct?
 
  • #4
Another possibility is that you might need and have forgotten a DB sign-on.
 
  • #5
SlurrerOfSpeech said:
Visual Studio is giving me the error

Error 1 'System.Data.Entity.DbSet<Survey.Models.Survey>.DbSet()' is inaccessible due to its protection level c:\users\jamin\documents\visual studio 2013\Projects\Survey\Survey\Models\SurveyDbModel.cs 83 40 Survey

on the line

Code:
DbSet<Survey> AllSurveys = new DbSet<Survey>();
...
I think it's because the constructor of DbSet<T> is protected and so its instance in this case should be created from the context object.
 

1. Why am I getting the error message "is inaccessible due to its protection level"?

The error message "is inaccessible due to its protection level" typically occurs when you are trying to access a variable or method that is declared as private or protected. This means that the variable or method can only be accessed within its own class or by subclasses. If you are trying to access it from a different class or outside of the inheritance hierarchy, you will receive this error message.

2. How can I fix the "is inaccessible due to its protection level" error?

To fix this error, you can change the access modifier of the variable or method to public. This will allow it to be accessed from any other class or outside the inheritance hierarchy. However, if the variable or method is meant to be restricted, you can also create a public method within the class that will return the value of the private variable or call the private method. This way, the data will still be protected but can be accessed through a public method.

3. Can I change the protection level of a variable or method from outside its class?

No, you cannot change the protection level of a variable or method from outside its class. The protection level can only be modified within the class itself. If you need to access the variable or method from outside the class, you will need to change its access modifier to public or create a public method to access it.

4. Is it possible to access a private or protected variable or method from a subclass?

Yes, it is possible to access a private or protected variable or method from a subclass. This is because subclasses have access to all protected and public members of their superclass. However, they cannot access private members, which can only be accessed within the superclass itself.

5. Why is it important to use proper access modifiers for variables and methods?

Using proper access modifiers for variables and methods is important for maintaining the integrity and security of your code. By restricting access to certain variables and methods, you can prevent unintended changes or access to sensitive data. This also helps with code organization and makes it easier to maintain and update your code in the future.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
5
Views
817
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
888
  • Programming and Computer Science
Replies
28
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
6
Views
5K
Back
Top