开发者

System.ArgumentOutOfRangeException: Index was out of range

开发者 https://www.devze.com 2023-02-20 12:50 出处:网络
I Have another Issue with my Code (ARGGGH!) I have a request.querys开发者_JS百科tring that I am calling and i get the following error Index was out of range. Must be non-negative and less than the siz

I Have another Issue with my Code (ARGGGH!) I have a request.querys开发者_JS百科tring 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


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"];


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号