user2469932
user2469932

Reputation: 167

'xx' is inaccessible due to its protection level

I have this error studentHelperClass.Form1.cmbBox is inaccessible due to its protection level

for this part of my code

class studentHC : Form1
{
    public studentHC()
    {
        InsertMethod();
    }

    private void InsertMethod()
    {
        MySqlConnection conn; // connection object;
        string connstring = 
                   "server=localhost;user Id=root;" + 
                   "database=collegesystem;Convert Zero Datetime=True ";
        conn = new MySqlConnection(connstring);
        conn.Open();
        using (var command = new MySqlCommand("SELECT * FROM person", conn))
        {
            using (var myReader = command.ExecuteReader())
            {
                cmbBox.Items.Add(myReader["personID"]);
            }
        }
    }

    internal static void insertMethod()
    {
        throw new NotImplementedException();
    }

the above code is for a SELECT query to display the contents of a table called person

and this I have in my Form

public partial class Form1 : Form
{
    MySqlConnection conn; // connection object;
    string connstring = 
               "server=localhost;user Id=root;" +
               "database=collegesystem;Convert Zero Datetime=True ";

    public Form1()
    {
        InitializeComponent();
        connection();
        selectStudent();
    }


    private void selectStudent()
    {
        try
        {
            studentHelperClass.studentHC.insertMethod();
        }

        catch (Exception err)
        {
            lblInfo.Text = " Error reading the database.";
            lblInfo.Text += err.Message;
        }
    }

How can I solve this error?

I believe that this is the last error before the program will work

EDIT:

this is thee only part of the code I didn't show you..and it has nothing to do with cmbBox :/

private void connection()
{
    try
    {
        conn = new MySqlConnection(connstring); //make the connection object
        conn.Open(); // try and open the connection
        lblInfo.Text = " server version: " + conn.ServerVersion;
        lblInfo.Text += "\n Connection is :" + conn.State.ToString();
    }
    catch (Exception err)
    {
        lblInfo.Text = " Error reading the database.";
        lblInfo.Text += err.Message; ;
    }

EDIT NUMBER 2:

private void InitializeComponent()
{
    this.cmbBox = new System.Windows.Forms.ComboBox();
    this.lblInfo = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // cmbBox
    // 
    this.cmbBox.FormattingEnabled = true;
    this.cmbBox.Location = new System.Drawing.Point(65, 9);
    this.cmbBox.Name = "cmbBox";
    this.cmbBox.Size = new System.Drawing.Size(121, 21);
    this.cmbBox.TabIndex = 0;

I have to change that to public?

Ok so I used the properties window to change cmbBox to protected and that removed the error, but now my label for statuses on the database has given me this error after I ran the program, any idea why? Error reading the database, method or operation is not implemented

Upvotes: 1

Views: 2210

Answers (4)

Hossain Muctadir
Hossain Muctadir

Reputation: 3626

I think you need to open auto generated partial class of Form1 and change cmbBox to protected. This can be done from the designer view also if you are using Visual Studio. This should solve the problem.

Upvotes: 3

devavx
devavx

Reputation: 1045

If the ComboBox is created by IDE,most probably the ComboBox will have been declared Private.Try setting it to Protected under category Modifiers in the Properties Window.

Upvotes: 2

Uatec
Uatec

Reputation: 141

Right click on the "cmbBox" object in the Form view. You need to set it's "Modifier" level to Public or Protected.

Upvotes: 1

dcastro
dcastro

Reputation: 68660

I suspect that cmbBox was declared as private (or maybe you didnt declare any protection level, and it defaults to private). If you can, please change it to be protected instead.

If you can't change it for some reason, try:

public partial class Form1 : Form
{
    protected void AddPerson(Person p)
    {
       cmbBox.Items.Add(p);
    }
}

and

class studentHC : Form1
{
    public studentHC()
    {
        InsertMethod();
    }

    private void InsertMethod()
    {
        MySqlConnection conn; // connection object;
        string connstring = "server=localhost;user Id=root;database=collegesystem;Convert Zero Datetime=True ";
        conn = new MySqlConnection(connstring);
        conn.Open();
        using (var command = new MySqlCommand("SELECT * FROM person", conn))
        {
            using (var myReader = command.ExecuteReader())
            {
                AddPerson(myReader["personID"]);
            }
        }
    }
}

Upvotes: 2

Related Questions