This.controls.find()[0]错误:索引超出了数组的范围

时间:2014-04-29 06:40:03

标签: c# mysql winforms

我有一个问题要问,我正在尝试使用rich textboxes将动态创建的sql database文本保存到This.controls.find()[0]。但是有一个错误返回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!");
    }

}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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();
    }