This.controls.find()[0]错误:索引在数组的边界之外

本文关键字:数组 索引 边界 错误 find controls This | 更新日期: 2023-09-27 18:00:34

我有一个问题要问,我正在尝试使用This.controls.find()[0]将动态创建的rich textboxes文本保存到sql database中。但存在返回Index was outside the bounds of the array的错误。我不知道该为数组设置什么,因为我没有。testing = (RichTextBox)this.Controls.Find("testing" + 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!");
    }

}

This.controls.find()[0]错误:索引在数组的边界之外

你的for循环应该像一样

for (int a = 0; a < i; a++)

此外,作为一个建议,您的btnSave_Click应该有以下更改

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

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

我认为您的a <= i应该是a < i