我有此错误studentHelperClass.Form1.cmbBox is inaccessible due to its protection level

对于我的代码的这一部分

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();
    }


上面的代码用于SELECT查询以显示称为person的表的内容

这是我的表格

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;
        }
    }


我该如何解决这个错误?

我相信这是程序正常运行之前的最后一个错误

编辑:

这只是你我没有给你看的代码的一部分..它与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; ;
    }


编辑编号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;


我必须将其更改为公开吗?

好的,所以我使用属性窗口将cmbBox更改为protected,并删除了该错误,但是现在我在运行程序后,数据库状态标签给了我这个错误,为什么?
Error reading the database, method or operation is not implemented

最佳答案

我认为您需要打开自动生成的Form1的部分类,并将cmbBox更改为protected。如果您使用的是Visual Studio,也可以从设计器视图中完成。这应该可以解决问题。

10-08 02:16