简体   繁体   English

System.ArgumentOutOfRangeException:索引超出范围

[英]System.ArgumentOutOfRangeException: Index was out of range

I Have another Issue with my Code (ARGGGH!) I have a request.querystring that I am calling and i get the following error Index was out of range. 我的代码有另一个问题(ARGGGH!),我正在调用request.querystring,并且得到以下错误索引超出范围。 Must be non-negative and less than the size of the collection. 必须为非负数并且小于集合的大小。 Parameter name: index 参数名称:索引

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

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

Any thoughts why? 有什么想法吗? I am trying to parse the account ref which is going to formatted like REDIT1 我正在尝试解析将要格式化为REDIT1的帐户ref

Cheers 干杯

Justin 贾斯汀

I'd bet that SqlDataSource1 had no parameters set, so your attempt to access the first (item 0) fails as the index must be in the range from 0 to Count-1 (which nothing satisfies in this case). 我敢打赌SqlDataSource1没有设置参数,因此您尝试访问第一个(项目0)的尝试失败,因为索引必须在0到Count-1的范围内(在这种情况下,这不能满足任何要求)。 You need to add the parameter. 您需要添加参数。

Also note that: 另请注意:

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

Is doubly redundant. 是双重冗余。 There's no need to cast the result of .ToString() to string, as ToString() always returns a string. 无需将.ToString()的结果.ToString()为字符串,因为ToString()始终返回字符串。

There's also no need to call it on the result of Request.Querystring[fieldName] as that also always returns a string. 也不需要在Request.Querystring[fieldName]的结果上调用它,因为它也总是返回一个字符串。 The following would suffice: 以下内容就足够了:

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

I got it! 我知道了! there was not a parameter set in my SQLDataSource! 我的SQLDataSource中没有设置参数!

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

Thanks guy's 多谢你们

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#System.ArgumentOutOfRangeException:索引超出范围 - C# System.ArgumentOutOfRangeException:Index was out of range System.ArgumentOutOfRangeException:索引超出范围 C# 中的错误 - System.ArgumentOutOfRangeException: Index was out of range Error in C# System.ArgumentOutOfRangeException: &#39;指定的参数超出了有效值的范围。 参数名称:asp.net中的index&#39; - System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Parameter name: index' in asp.net 错误:System.ArgumentOutOfRangeException:索引超出范围。 必须为非负数且小于集合的大小 - Error: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection System.ArgumentOutOfRangeException: '索引超出范围。 从一个列表复制到另一个 - System.ArgumentOutOfRangeException: 'Index was out of range. copy from one list to another System.ArgumentOutOfRangeException:&#39;索引超出范围。 必须为非负数,并且小于集合的大小。” - System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.' ListView异常:System.ArgumentOutOfRangeException:参数超出范围 - ListView exception: System.ArgumentOutOfRangeException: Argument is out of range System.ArgumentOutOfRangeException: &#39;索引超出范围。 必须是非负的并且小于集合的大小。 参数名称:索引&#39; - System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index' System.ArgumentOutOfRangeException:索引超出范围。 必须是非负的并且小于集合的大小。 参数名称:索引 - System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index System.ArgumentOutOfRangeException 索引超出范围。 必须是非负数且小于集合的大小。 参数名称:索引 - System.ArgumentOutOfRangeException Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM