C# System.ArgumentOutOfRangeException:索引超出范围

C# System.ArgumentOutOfRangeException:索引超出范围,c#,request.querystring,C#,Request.querystring,我的代码有另一个问题(ARGGGH!)我调用了一个request.querystring,出现以下错误索引超出范围。必须为非负数且小于集合的大小。参数名称:索引 public void getAccountRef() { string getAccountRef = (string)Request.QueryString["AccountRef"].ToString(); SqlDataSource1.SelectParameters[0].DefaultValue = get

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

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

    SqlDataSource1.SelectParameters[0].DefaultValue = getAccountRef;
}
你知道为什么吗?我正在尝试解析帐户ref,它的格式将类似于REDIT1

干杯


贾斯汀,我明白了!我的SQLDataSource中没有设置参数

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


感谢guy的

我敢打赌SqlDataSource1没有设置任何参数,因此您尝试访问第一个(项目0)失败,因为索引必须在0到Count-1的范围内(在本例中没有任何内容满足)。您需要添加参数

还请注意:

string getAccountRef = (string)Request.QueryString["AccountRef"].ToString()
这是双重冗余。无需将
.ToString()
的结果强制转换为字符串,因为
ToString()
始终返回字符串

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

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

这意味着
SqlDataSource1
没有索引为
0