Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using a label in gridview which has a text value as Text='<%#Container.DataItemIndex+1 %>'

When the gridview is empty, I get the following error as
SQL
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


I know that Label doesn't accept 0. So how to solve this problem ??
Posted
Comments
Siva Hyderabad 24-Feb-14 2:17am    
post your aspx code?
Sriram Ramachandran 24-Feb-14 2:23am    
<asp:TemplateField HeaderText="S.No">

<itemtemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Container.DataItemIndex+1 %>'>

<itemstyle horizontalalign="Left" width="10px" height="20px">


<asp:BoundField DataField="From" HeaderText="From" >
<itemstyle horizontalalign="Left" height="20px">


<asp:TemplateField HeaderText="Subject" ItemStyle-HorizontalAlign="Center" ControlStyle-ForeColor="Blue">
<itemtemplate>
<asp:HiddenField ID="hdnMailID" runat="server" Value='<%#Eval("Mail_Msg_ID")%>' />
<asp:LinkButton ID="lbView" OnClick="lbView_Click" runat="server" Text='<%#Eval("Subject")%>'>

<itemstyle horizontalalign="Left" height="20px">

1 solution

Remove the label.Try this:

ASP.NET
<asp:TemplateField HeaderText="S No">
        <ItemTemplate>
           <%#Container.DataItemIndex +1 %>
        </ItemTemplate>
        </asp:TemplateField>


Label is not needed here
 
Share this answer
 
v2
Comments
Sriram Ramachandran 24-Feb-14 2:25am    
thanks ... I ll try and reply u asap
Tom Marvolo Riddle 24-Feb-14 2:25am    
try it and let me know
Sriram Ramachandran 24-Feb-14 2:26am    
But I ve a doubt ... The SNO has values 1,2,3 and how to fetch those numbers using ITEMTEMPLATE...
Tom Marvolo Riddle 24-Feb-14 2:28am    
sorry i didn't get it.what is the need of fetching SNO.Please explain bit more
Tom Marvolo Riddle 24-Feb-14 2:30am    
This is just used to show the total no records in gridview.any issues feel free to ask

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