Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey i m trying to delete the row from the grid view but i m getting an error
Must be non-negative and less than the size of the collection. Parameter name: index

please help me out thanks in advance

C#
string Id = GridView1.DataKeys[e.RowIndex].Value.ToString();
           
            string s = "DELETE Product_Info where product_info.Id ="+Id+"";

            BindGridData();
Posted
Updated 9-Aug-12 10:27am
v2
Comments
StianSandberg 9-Aug-12 16:25pm    
Your code is vulnerable for sql injections
http://www.codeproject.com/Articles/425150/Beginners-guide-to-a-secure-way-of-storing-passwor#sqlinjections

The error is because e.RowIndex gives you a number which is larger than the number og datakeys. Or datakeys has no values. Try your debugger..

'e.RowIndex' is likely trying to access an index that there is no data for. My guess would be it is '-1' Use debugger and check the value of e.RowIndex. Only attempt to execute this portion of code if it is valid.
 
Share this answer
 
v2
You can specify a specific cell in a GridView. In your code, you can just replace:

C#
GridView1.DataKeys[e.RowIndex].Values

with:
C#
GridView1.Rows[e.RowIndex].Cells[0].Text



--Amit
 
Share this answer
 
C#
string Id = GridView1.Rows[e.RowIndex].Cells[your_current_colum_index].Text;
           
            string s = "DELETE Product_Info where product_info.Id ="+Id+"";
 
            BindGridData();


The error is not in e.RowIndex. If you breakpoint the first line you'll see what I talking about.

So, I think the error is in DataKey collection (is null). When you try to get the value from the element inside DataKey exception appears.

Hope it helps
 
Share this answer
 
v2
Comments
Christian Amado 9-Aug-12 17:43pm    
And the downvoted was for???
Philip Stuyck 9-Aug-12 17:56pm    
Don't know looks correct to me +5 from me.
Christian Amado 9-Aug-12 17:57pm    
Thanks. All post that I wrote has tested on my pc =)
AmitGajjar 9-Aug-12 23:37pm    
Someone downvote you possibly because the error message says index out of range. so problem is with RowIndex and you have not mention anything like that. this can be the reason.
Christian Amado 10-Aug-12 6:58am    
RowIndex isn't the problem here.

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