Click here to Skip to main content
15,896,544 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI ALL,

I have got an error on Index was outside the bounds of the array.

here is the error code:




Line 21: if (e.CommandName == "view")
Line 22: {
Line 23: Response.Redirect("~/ViewDetails.aspx?val=" + ListView1.DataKeyNames[e.Item.DataItemIndex].ToString());
Line 24: }
Line 25: }
Posted

We can't absolutely solve that for you - it's obvious why you get the error: e.Item.DataItemIndex is negative or greater-than-or-equal-to than the number of items in ListView1.DataKeyNames

But why it is it down to you! Use the debugger: put a breakpoint on the line and run your code. when the breakpoint hits, look at the variables and see what value you have, and how big the array is. Then you can start to work backwards to find out why - but we can't because we don't have access to your data!
 
Share this answer
 
Comments
[no name] 4-Dec-13 7:03am    
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="_Default" MasterPageFile="~/MasterPage.master" %>

<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="main">

<div id="Div1">
<asp:ListView ID="ListView1" runat="server" DataKeyNames="adid"
DataSourceID="SqlDataSource1"
onselectedindexchanged="ListView1_SelectedIndexChanged"
ViewStateMode="Enabled" onitemcommand="ListView1_ItemCommand">


<emptydatatemplate>
No item found.....

<itemtemplate>
<div class="wrap">
<div class="t"><asp:Label ID="adstitleLabel" runat="server" Text='<%# Eval("adcategory") %>' /></div>
<div class="img"><img id="Img1" src='<%# "~/ads/" + Eval("adimage") %>' alt="" title="" runat="server" /></div>
<div class="t"><asp:Label ID="Label1" runat="server" Text='<%# Eval("addes") %>' /></div>
<div class="t">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="view">View Details</div>
</div>



</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:adswebConnectionString %>"
SelectCommand="SELECT * FROM [ads1] WHERE (adcategory LIKE @adcat + '%') OR (subcat LIKE @adcat + '%')">
<SelectParameters>
<asp:QueryStringParameter Name="adcat" QueryStringField="item" />
</SelectParameters>

OriginalGriff 4-Dec-13 7:10am    
Yes - you need to look, not us - we don't have access to your database, you do!
It is saying that the value of e.Item.DataItemIndex is greater than the upper bound index of ListView1.DataKeyNames array.

for example if the size of ListView1.DataKeyNames is 7, then its legal indexes will be 0,1,2,...6.

if e.Item.DataItemIndex is 7 then this error will be thrown.
 
Share this answer
 
Comments
[no name] 4-Dec-13 7:03am    
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="_Default" MasterPageFile="~/MasterPage.master" %>

<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="main">

<div id="Div1">
<asp:ListView ID="ListView1" runat="server" DataKeyNames="adid"
DataSourceID="SqlDataSource1"
onselectedindexchanged="ListView1_SelectedIndexChanged"
ViewStateMode="Enabled" onitemcommand="ListView1_ItemCommand">


<emptydatatemplate>
No item found.....

<itemtemplate>
<div class="wrap">
<div class="t"><asp:Label ID="adstitleLabel" runat="server" Text='<%# Eval("adcategory") %>' /></div>
<div class="img"><img id="Img1" src='<%# "~/ads/" + Eval("adimage") %>' alt="" title="" runat="server" /></div>
<div class="t"><asp:Label ID="Label1" runat="server" Text='<%# Eval("addes") %>' /></div>
<div class="t">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="view">View Details</div>
</div>



</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:adswebConnectionString %>"
SelectCommand="SELECT * FROM [ads1] WHERE (adcategory LIKE @adcat + '%') OR (subcat LIKE @adcat + '%')">
<SelectParameters>
<asp:QueryStringParameter Name="adcat" QueryStringField="item" />
</SelectParameters>

Hi Try like this..

You are trying to access the index value of collection which is not available, so its better to check the index available before accessing.

C#
int index = e.Item.DataItemIndex;
              int length = ListView1.DataKeyNames.Length;
              if( index < length)
                  Response.Redirect("~/ViewDetails.aspx?val=" + ListView1.DataKeyNames[index].ToString());
 
Share this answer
 
Comments
[no name] 4-Dec-13 7:03am    
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="_Default" MasterPageFile="~/MasterPage.master" %>

<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="main">

<div id="Div1">
<asp:ListView ID="ListView1" runat="server" DataKeyNames="adid"
DataSourceID="SqlDataSource1"
onselectedindexchanged="ListView1_SelectedIndexChanged"
ViewStateMode="Enabled" onitemcommand="ListView1_ItemCommand">


<emptydatatemplate>
No item found.....

<itemtemplate>
<div class="wrap">
<div class="t"><asp:Label ID="adstitleLabel" runat="server" Text='<%# Eval("adcategory") %>' /></div>
<div class="img"><img id="Img1" src='<%# "~/ads/" + Eval("adimage") %>' alt="" title="" runat="server" /></div>
<div class="t"><asp:Label ID="Label1" runat="server" Text='<%# Eval("addes") %>' /></div>
<div class="t">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="view">View Details</div>
</div>



</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:adswebConnectionString %>"
SelectCommand="SELECT * FROM [ads1] WHERE (adcategory LIKE @adcat + '%') OR (subcat LIKE @adcat + '%')">
<SelectParameters>
<asp:QueryStringParameter Name="adcat" QueryStringField="item" />
</SelectParameters>

Thanks7872 4-Dec-13 23:21pm    
And why you posted this code dump to each and every comment?
Please debug your code so that you will be able to know for what reason the index is going outside the range.

Make sure that e.Item.DataItemIndex is a valid integer.
Make sure that ListView1.DataKeyNames array contains item for that DataIndex.
 
Share this answer
 
Comments
[no name] 4-Dec-13 7:01am    
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="_Default" MasterPageFile="~/MasterPage.master" %>

<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="main">

<div id="Div1">
<asp:ListView ID="ListView1" runat="server" DataKeyNames="adid"
DataSourceID="SqlDataSource1"
onselectedindexchanged="ListView1_SelectedIndexChanged"
ViewStateMode="Enabled" onitemcommand="ListView1_ItemCommand">


<emptydatatemplate>
No item found.....

<itemtemplate>
<div class="wrap">
<div class="t"><asp:Label ID="adstitleLabel" runat="server" Text='<%# Eval("adcategory") %>' /></div>
<div class="img"><img id="Img1" src='<%# "~/ads/" + Eval("adimage") %>' alt="" title="" runat="server" /></div>
<div class="t"><asp:Label ID="Label1" runat="server" Text='<%# Eval("addes") %>' /></div>
<div class="t">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="view">View Details</div>
</div>



</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:adswebConnectionString %>"
SelectCommand="SELECT * FROM [ads1] WHERE (adcategory LIKE @adcat + '%') OR (subcat LIKE @adcat + '%')">
<SelectParameters>
<asp:QueryStringParameter Name="adcat" QueryStringField="item" />
</SelectParameters>

So, tell me on which event and on which scenario you are facing the issue?
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
string ID =  ListView1.DataKeys[dataItem.DisplayIndex].Value.ToString();
Response.Redirect("~/ViewDetails.aspx?val=" + ID);
 
Share this answer
 
Comments
[no name] 4-Dec-13 7:28am    
Thanks all of you....Thanks sankarsan parida I have used your code,Its working now..Thanks a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900