我想生成动态标签,并通过数据库获取其文本。我在第"Index was outside the bounds of the array."
行收到此错误l.Name = split[j] + i.ToString();
我想显示5个标签。我要通过数据库读取数组中的5条记录,我想在每个标签上显示每个记录。我无法弄清楚这段代码有什么问题。帮帮我。我已经使用split函数来拆分数组。
private void button1_Click(object sender, EventArgs e)
{
int start = 232;
int end = 100;
for(int i=0;i<5;i++)
{
Label l = addlabel(i, start, end);
this.Controls.Add(l);
end += 30;
}
int start1 = 353;
int end1 = 100;
for (int i1 = 0; i1 < 5; i1++)
{
Label l = addlabel1(i1, start1, end1);
this.Controls.Add(l);
end += 30;
}
}
Label addlabel(int i,int start,int end)
{
string cons = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
SqlConnection con = new SqlConnection(cons);
con.Open();
string str = "SELECT * FROM marks WHERE idno like '%" + textBox1.Text + "%'";
SqlCommand com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
var input = reader["subjects"].ToString();
var split = input.Split(new string[] { ",", " " }, StringSplitOptions.RemoveEmptyEntries);
for (int j = 0; j <= split.Length; j++)
{
Label l = new Label();
l.Name = "label" + i.ToString();
l.Text = split[j] + i.ToString();
}
}
return label1;
}
Label addlabel1(int i1, int start1, int end1)
{
string cons = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
SqlConnection con = new SqlConnection(cons);
con.Open();
string str = "SELECT * FROM marks WHERE idno like '%" + textBox1.Text + "%'";
SqlCommand com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
var input1 = reader["smarks"].ToString();
var split1 = input1.Split(new string[] { ",", " " }, StringSplitOptions.RemoveEmptyEntries);
for (int z = 0; z <= split1.Length; z++)
{
Label l = new Label();
l.Name = "label" + i1.ToString();
l.Text = split1[z] + i1.ToString();
}
}
return label1;
}
任何建议我该怎么办?
答案 0 :(得分:3)
您在这里有错误
for (int j = 0; j <= split.Length; j++)
{
Label l = new Label();
l.Name = "label" + i.ToString();
l.Text = split[j] + i.ToString();
}
您必须做到
for (int j = 0; j <split.Length; j++)
如果您有5个元素,则split.Length
给您5。但是它存储在索引0到4中。如果您给j <=split.Length
,它的工作范围是从j=0
到{{1} }。和j=5
会给您这个错误
答案 1 :(得分:1)
正如其他答案所建议的那样,更改循环中的条件将解决您的问题。但是真正的问题只在那之后。您还必须注意以下几点,以使您的代码更好。
subjects
字段,而第二种情况中仅使用smarks
字段,这就是为什么要使用*
来获取全部内容的原因。总是只从数据库中获取必填字段。addlabel
无需返回任何内容,您可以在方法本身内创建并添加动态标签。 您的代码将如下所示,其中包括我在此处提到的所有更改
void addlabel(int i, int start, int end)
{
string conStr = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
using (SqlConnection conObject = new SqlConnection(conStr))
{
conObject.Open();
string querySql = "SELECT subjects FROM marks WHERE idno like @idInputs";
using (SqlCommand cmdSql = new SqlCommand(querySql, conObject))
{
cmdSql.Parameters.Add(" @idInputs", SqlDbType.VarChar).Value = "%" + textBox1.Text + "%";
using (SqlDataReader reader = cmdSql.ExecuteReader())
{
while (reader.Read())
{
var input = reader["subjects"].ToString();
var split = input.Split(new string[] { ",", " " }, StringSplitOptions.RemoveEmptyEntries);
for (int j = 0; j < split.Length; j++)
{
Label newLabel = new Label();
newLabel.Name = "label" + i.ToString();
newLabel.Text = split[j] + i.ToString();
this.Controls.Add(newLabel);
}
}
}
}
}
}
答案 2 :(得分:0)
您的问题是您的for循环多次循环。如果您拆分了3个元素,则会从0循环到3,即4次。
将for循环更改为
for (int j = 0; j < split.Length; j++)
循环正确的次数