c# - 'xx' 由于其保护级别而无法访问

标签 c# winforms

我有这个错误 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,这也可以从设计器 View 中完成。这应该可以解决问题。

关于c# - 'xx' 由于其保护级别而无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18719524/

相关文章:

c# - 防止将数字粘贴到 .net Windows 窗体中的文本框中

c# - 如何从 C# WinForms 应用程序判断 SQL Server 自动升级是否成功

c# - 如何检查对象是否已实例化?

c# - 为什么使用 DataTemplates 时 ViewModel 的 ObservableCollection 没有显示在 View 中?

C# 静态类与 VB.NET 模块使用

c# - C#调用ShowDialog(ParentForm)时触发什么事件

c# - 是否可以禁用基于 .NET 版本的部分程序?

winforms - 为 DevExpress XtraGrid 创建 BandedGridView

c# - 根据 ID 从列表中查找项目

C# 返回类型推断不好的做法?