System.ArgumentOutOfRangeException:索引超出范围

发布于 2024-10-27 12:52:21 字数 392 浏览 6 评论 0原文

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

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

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

有什么想法吗?我正在尝试解析帐户引用,其格式类似于 REDIT1

Cheers

Justin

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. 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

Cheers

Justin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫烠⑼条掵仅有一顆心 2024-11-03 12:52:21

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

另请注意:

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

是双重冗余的。无需将 .ToString() 的结果转换为字符串,因为 ToString() 始终返回字符串。

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

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

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). 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.

There's also no need to call it on the result of Request.Querystring[fieldName] as that also always returns a string. The following would suffice:

string getAccountRef = Request.QueryString["AccountRef"];
深陷 2024-11-03 12:52:21

我得到了它!我的 SQLDataSource 中没有设置参数!

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

谢谢那家伙的

I got it! there was not a parameter set in my SQLDataSource!

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

Thanks guy's

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文