【发布时间】: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];抱歉没有说错误在哪里。我编辑了问题。