【问题标题】:Index was outside the bounds of the array split function [duplicate]索引超出了数组拆分函数的范围[重复]
【发布时间】:2018-06-29 05:38:38
【问题描述】:

我想生成动态标签,其文本通过数据库获取。我在l.Name = split[j] + i.ToString(); 行收到此错误"Index was outside the bounds of the array." 我想显示 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;
}

有什么建议吗?

【问题讨论】:

  • 一个建议:停止连接 SQL 查询,改用参数。查找 SQL 注入
  • 调试你的代码,尤其是在你循环分割字符串的地方。观察你试图访问的索引

标签: c# winforms


【解决方案1】:

这里有错误

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 &lt;=split.Length,它从 j=0j=5 工作。和split[5] 给你这个错误

【讨论】:

    【解决方案2】:

    正如其他答案所暗示的,更改循环中的条件将解决您的问题。但真正的问题只是在那之后。您还必须关心以下事项以使您的代码更好。

    • 使用连接字符串作为查询就像将您的储物柜钥匙交给黑客一样。最好使用参数化。
    • 对于您使用subjects 字段的第一种情况和您仅使用smarks 字段的第二种情况,他们为什么要使用* 获取全部内容。始终只从数据库中获取必填字段。
    • 利用 using 语句自动处理对象。
    • 根据此实现,方法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);
                         }
                    }
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      您的问题是您的 for 循环循环一次到多次。如果拆分有 3 个元素,则会从 0 循环到 3,即 4 次。

      将for循环改为

      for (int j = 0; j < split.Length; j++)
      

      循环正确的次数

      【讨论】:

      • 它工作了...我没有收到任何错误,但现在点击按钮后什么都没有显示
      猜你喜欢
      • 1970-01-01
      • 2011-04-11
      • 2012-01-31
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多