【问题标题】:This.controls.find()[0] Error: Index was outside the bound of arrayThis.controls.find()[0] 错误:索引超出数组范围
【发布时间】:2014-04-29 06:40:02
【问题描述】:

我有一个问题要问,我正在尝试使用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!");
    }

}

【问题讨论】:

  • 究竟在哪一行?调试您的代码,您可以轻松找到错误并修复它。
  • 好吧测试发生错误 = (RichTextBox)this.Controls.Find("testing" + a.ToString(), true)[0];抱歉没有说错误在哪里。我编辑了问题。

标签: c# mysql winforms


【解决方案1】:

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

【讨论】:

  • 好吧,丢失的不是最后一个数据,而是第二个数据。
  • 我正在获取所有记录,但我没有找到任何数据丢失的原因:(
  • 好吧,我不能去聊天,声望不够,哈哈,他们叫我去聊天~.~
  • 好吧,我还有名为 testing0 的静态富文本框。我使用 ++ 来添加自动增量。这会影响结果吗?
  • 好吧,我尝试上传我的 ui 图像,但显然我也没有足够的声誉 T.T
【解决方案2】:

我认为您的a &lt;= i 应该是a &lt; i

【讨论】:

  • 我试过了。但是数据库总是比我输入的少1。但是,是的,它解决了出现越界错误的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-13
  • 2013-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多