C# This.controls.find()[0]错误:索引超出了数组的界限

C# This.controls.find()[0]错误:索引超出了数组的界限,c#,mysql,winforms,C#,Mysql,Winforms,我有一个问题要问,我正在尝试使用此.controls.find[0]将动态创建的富文本框文本保存到sql数据库中。但是有一个错误,返回索引超出了数组的边界。我不知道应该为数组设置什么,因为我没有数组。测试时出错=RichTextBoxthis.Controls.Findtesting+a.ToString,true[0];请帮帮我。先谢谢你。代码如下: public Form1() { InitializeComponent(); } RichTe

我有一个问题要问,我正在尝试使用此.controls.find[0]将动态创建的富文本框文本保存到sql数据库中。但是有一个错误,返回索引超出了数组的边界。我不知道应该为数组设置什么,因为我没有数组。测试时出错=RichTextBoxthis.Controls.Findtesting+a.ToString,true[0];请帮帮我。先谢谢你。代码如下:

   public Form1()
    {
        InitializeComponent();
    }

    RichTextBox testing = new RichTextBox();
    RichTextBox lol = new RichTextBox();
    ComboBox haha = new ComboBox();
    int i = 0;

    private void btnAdd_Click(object sender, EventArgs e)
    {
        int rows = this.tableLayoutPanel1.RowCount + 1;
        tableLayoutPanel1.SuspendLayout();
        testing = new RichTextBox();
        testing.Name ="testing"+ i.ToString();
        testing.Width = 227;
        testing.Height = 96;
        tableLayoutPanel1.Controls.Add(testing, 0, rows + 1);

        lol = new RichTextBox();
        lol.Name = "lol" + i.ToString();
        lol.Width = 227;
        lol.Height = 96;
        tableLayoutPanel1.Controls.Add(lol, 1, rows + 1);

        haha = new ComboBox();
        haha.Name = "haha" + i.ToString();
        haha.Items.Insert(0, "Visibility of system status");
        haha.Items.Insert(1, "Match between system and the real world");
        haha.DropDownStyle = ComboBoxStyle.DropDownList;
        haha.Width = 224;
        haha.Height = 21;
        tableLayoutPanel1.Controls.Add(haha, 2, rows + 1);

        tableLayoutPanel1.RowCount++;
        i++;
        tableLayoutPanel1.ResumeLayout();
    }


    private void btnSave_Click(object sender, EventArgs e)
    {
        for (int a = 0; a <= i; a++)
        {
            string strConnectionString = ConfigurationManager.ConnectionStrings["HeuristicDatabaseConnectionString"].ConnectionString;

            SqlConnection myconnection = new SqlConnection(strConnectionString);

            String strCommandText = "INSERT Form(Location,Violation,Recommendation)"
               + " VALUES(@Location,@Violation,@Recommendation)";
            SqlCommand Cmd = new SqlCommand(strCommandText, myconnection);

            testing = (RichTextBox)this.Controls.Find("testing" + a.ToString(), true)[0];
            Cmd.Parameters.AddWithValue(@"Location", testing.Text);

            lol = (RichTextBox)this.Controls.Find("lol" + a.ToString(), true)[0];
            Cmd.Parameters.AddWithValue(@"Violation", lol.Text);

            haha = (ComboBox)this.Controls.Find("haha" + a.ToString(), true)[0];
            Cmd.Parameters.AddWithValue(@"Recommendation", haha.SelectedItem);

            myconnection.Open();

            Cmd.ExecuteNonQuery();
            myconnection.Close();
        }

        MessageBox.Show("Data added into database!");
    }
编辑:现在没有错误索引超出数组的范围,但在将数据添加到数据库中时出现问题,第二个数据将永远不会记录到数据库中,此问题的原因是什么??请帮忙。代码如下:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    RichTextBox testing = new RichTextBox();
    RichTextBox lol = new RichTextBox();
    ComboBox haha = new ComboBox();
    int i = 0;

    private void btnAdd_Click(object sender, EventArgs e)
    {
        int rows = this.tableLayoutPanel1.RowCount + 1;
        tableLayoutPanel1.SuspendLayout();
        testing = new RichTextBox();
        testing.Name ="testing"+ i.ToString();
        testing.Width = 227;
        testing.Height = 96;
        tableLayoutPanel1.Controls.Add(testing, 0, rows + 1);

        lol = new RichTextBox();
        lol.Name = "lol" + i.ToString();
        lol.Width = 227;
        lol.Height = 96;
        tableLayoutPanel1.Controls.Add(lol, 1, rows + 1);

        haha = new ComboBox();
        haha.Name = "haha" + i.ToString();
        haha.Items.Insert(0, "Visibility of system status");
        haha.Items.Insert(1, "Match between system and the real world");
        haha.DropDownStyle = ComboBoxStyle.DropDownList;
        haha.Width = 224;
        haha.Height = 21;
        tableLayoutPanel1.Controls.Add(haha, 2, rows + 1);

        tableLayoutPanel1.RowCount++;
        tableLayoutPanel1.ResumeLayout();

        i++;
    }


    private void btnSave_Click(object sender, EventArgs e)
    {
        string strConnectionString = ConfigurationManager.ConnectionStrings["HeuristicDatabaseConnectionString"].ConnectionString;

        SqlConnection myconnection = new SqlConnection(strConnectionString);

        String strCommandText = "INSERT Form(Location,Violation,Recommendation)"
           + " VALUES(@Location,@Violation,@Recommendation)";
        SqlCommand Cmd = new SqlCommand(strCommandText, myconnection);

        myconnection.Open();

        for (int a = 0; a < i; a++)
        {
            Cmd.Parameters.Clear();

            testing = (RichTextBox)this.Controls.Find("testing" + a.ToString(), true)[0];
            Cmd.Parameters.AddWithValue("@Location", testing.Text);

            lol = (RichTextBox)this.Controls.Find("lol" + a.ToString(), true)[0];
            Cmd.Parameters.AddWithValue("@Violation", lol.Text);

            haha = (ComboBox)this.Controls.Find("haha" + a.ToString(), true)[0];
            Cmd.Parameters.AddWithValue("@Recommendation", haha.SelectedItem);

            Cmd.ExecuteNonQuery();
        }

        myconnection.Close();

        MessageBox.Show("Data added into database!");
    }
解决方案:

 public partial class Form1 : Form
    {
       public Form1()
         {
            InitializeComponent();
         }

    RichTextBox testing = new RichTextBox();
    RichTextBox lol = new RichTextBox();
    ComboBox haha = new ComboBox();
    int i = 1;

    private void btnAdd_Click(object sender, EventArgs e)
    {
        int rows = this.tableLayoutPanel1.RowCount + 1;
        tableLayoutPanel1.SuspendLayout();
        testing = new RichTextBox();
        testing.Name ="testing"+ i.ToString();
        testing.Width = 227;
        testing.Height = 96;
        tableLayoutPanel1.Controls.Add(testing, 0, rows + 1);

        lol = new RichTextBox();
        lol.Name = "lol" + i.ToString();
        lol.Width = 227;
        lol.Height = 96;
        tableLayoutPanel1.Controls.Add(lol, 1, rows + 1);

        haha = new ComboBox();
        haha.Name = "haha" + i.ToString();
        haha.Items.Insert(0, "Visibility of system status");
        haha.Items.Insert(1, "Match between system and the real world");
        haha.DropDownStyle = ComboBoxStyle.DropDownList;
        haha.Width = 224;
        haha.Height = 21;
        tableLayoutPanel1.Controls.Add(haha, 2, rows + 1);

        tableLayoutPanel1.RowCount++;
        tableLayoutPanel1.ResumeLayout();

        i++;
    }


    private void btnSave_Click(object sender, EventArgs e)
    {
        string strConnectionString = ConfigurationManager.ConnectionStrings["HeuristicDatabaseConnectionString"].ConnectionString;

        SqlConnection myconnection = new SqlConnection(strConnectionString);

        String strCommandText = "INSERT Form(Location,Violation,Recommendation)"
           + " VALUES(@Location,@Violation,@Recommendation)";
        SqlCommand Cmd = new SqlCommand(strCommandText, myconnection);

        myconnection.Open();

        //This are richtextboxes which are created at design time.
        Cmd.Parameters.AddWithValue("@Location", testing0.Text);
        Cmd.Parameters.AddWithValue("@Location", lol0.Text);
        Cmd.Parameters.AddWithValue("@Location", haha0.SelectedItem);

        for (int a = 0; a < i; a++)
        {
            Cmd.Parameters.Clear();

            //This are to retrieve texts from the textboxes that are created during runtime
            //And add it into the database.
            testing = (RichTextBox)this.Controls.Find("testing" + a.ToString(), true)[0];
            Cmd.Parameters.AddWithValue("@Location", testing.Text);

            lol = (RichTextBox)this.Controls.Find("lol" + a.ToString(), true)[0];
            Cmd.Parameters.AddWithValue("@Violation", lol.Text);

            haha = (ComboBox)this.Controls.Find("haha" + a.ToString(), true)[0];
            Cmd.Parameters.AddWithValue("@Recommendation", haha.SelectedItem);

            Cmd.ExecuteNonQuery();
        }

        myconnection.Close();

        MessageBox.Show("Data added into database!");
    }

}我认为你的afor循环应该是这样的

for (int a = 0; a < i; a++)
另外,作为建议,您的btnSave_单击应该有以下更改

仅在循环外部声明、打开和关闭连接一次 在使用参数化命令时,请声明该命令一次,并在每个循环中添加参数,然后执行

private void btnSave_Click(object sender, EventArgs e)
{
    string strConnectionString = ConfigurationManager.ConnectionStrings["HeuristicDatabaseConnectionString"].ConnectionString;

    SqlConnection myconnection = new SqlConnection(strConnectionString);

    String strCommandText = "INSERT Form(Location,Violation,Recommendation)"
       + " VALUES(@Location,@Violation,@Recommendation)";
    SqlCommand Cmd = new SqlCommand(strCommandText, myconnection);

    myconnection.Open();

    for (int a = 0; a <= i; a++)
    {
        Cmd.Parameters.Clear();    // clear the parameters so that previous values are cleared

        testing = (RichTextBox)this.Controls.Find("testing" + a.ToString(), true)[0];
        Cmd.Parameters.AddWithValue(@"Location", testing.Text);

        lol = (RichTextBox)this.Controls.Find("lol" + a.ToString(), true)[0];
        Cmd.Parameters.AddWithValue(@"Violation", lol.Text);

        haha = (ComboBox)this.Controls.Find("haha" + a.ToString(), true)[0];
        Cmd.Parameters.AddWithValue(@"Recommendation", haha.SelectedItem);

        Cmd.ExecuteNonQuery();
    }
    myconnection.Close();
}

具体在哪一行?调试代码,您可以轻松找到错误并修复它。好的,错误发生在testing=RichTextBoxthis.Controls.Findtesting+a.ToString,true[0];对不起,我没有说错误在哪里。我编辑了问题,我试过了。但是数据库总是比我输入的少保存1。但是,是的,它解决了越界错误的问题。好吧,丢失的不是最后一个数据,而是第二个数据。我正在获取所有记录,我没有找到丢失任何数据的原因:我不能去聊天,声誉不够,哈哈,他们打电话给我去聊天~。~好吧,我仍然有静态richtextbox名为testing0。我使用了++来像自动增量一样添加。这会影响结果吗?我试着上传我的用户界面的图片,但显然我也没有足够的声誉