【问题标题】:System.ArgumentOutOfRangeException: Index was out of rangeSystem.ArgumentOutOfRangeException:索引超出范围
【发布时间】:2011-03-28 15:45:30
【问题描述】:

我的代码有另一个问题(ARGGGH!)我有一个我正在调用的 request.querystring,我收到以下错误 索引超出范围。必须是非负数且小于集合的大小。参数名称:索引

public void getAccountRef()
{
    string getAccountRef = (string)Request.QueryString["AccountRef"].ToString();

    SqlDataSource1.SelectParameters[0].DefaultValue = getAccountRef;
}

任何想法为什么?我正在尝试解析将格式化为 REDIT1 的帐户引用

干杯

贾斯汀

【问题讨论】:

  • 这意味着SqlDataSource1 没有索引为0 的选择参数

标签: c# request.querystring


【解决方案1】:

我敢打赌 SqlDataSource1 没有设置参数,因此您访问第一个(第 0 项)的尝试失败,因为索引必须在 0 到 Count-1 的范围内(在这种情况下不满足任何条件)。您需要添加参数。

还要注意:

string getAccountRef = (string)Request.QueryString["AccountRef"].ToString()

双重冗余。不需要将.ToString() 的结果转换为字符串,因为ToString() 总是返回一个字符串。

也没有必要在Request.Querystring[fieldName] 的结果上调用它,因为它也总是返回一个字符串。以下内容就足够了:

string getAccountRef = Request.QueryString["AccountRef"];

【讨论】:

    【解决方案2】:

    我明白了!我的 SQLDataSource 中没有设置参数!

    <SelectParameters>
        <asp:Parameter DefaultValue="1" Name="AccountRef" Type="String" />
    </SelectParameters>
    

    谢谢大佬

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多