Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, when i am deleting the row from gridview then getting errors-Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index i have a code-
C#
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string autoID = GridView1.DataKeys[e.RowIndex].Value.ToString();
        SqlConnection con = ConnectionClass.GetConnection();
        SqlCommand cmd=new SqlCommand("delete from information where id='"+ Convert.ToInt32(autoID) + "'", con);
        cmd.ExecuteNonQuery();
        con.Close();
        ShowData();
    }



Plz give me a solution
Posted
Updated 13-Jul-12 2:25am
v2

Integer value need not ''
SQL
SqlCommand cmd=new SqlCommand("delete from information where id="+ Convert.ToInt32(autoID) + "", con);

Have you checked the value of autoID

Please check the value of autoID by using DEBUGGER.

Thanks
Ashish
 
Share this answer
 
Comments
[no name] 13-Jul-12 8:30am    
my 5+
AshishChaudha 13-Jul-12 8:31am    
Thanks Mits
Shemeer NS 13-Jul-12 9:43am    
5'ed for good guidance...
AshishChaudha 13-Jul-12 9:59am    
thanx
Manas Bhardwaj 16-Jul-12 5:56am    
5!
I think you have multiple datakeys defined for this gridview

DataKeyName="autoID,anotherColumn"

If this is your case then try specifying the index of the key in your code like below
C#
GridView1.DataKeys[e.RowIndex].Value[0].ToString();
 
Share this answer
 
Hi,

i thing the error is from the DataKeys collection.
if you can get autoID as one of the gridview columns then simply you can use like this..

C#
//here 0 may be replaced with the column index of the autoID inside gridview.
string autoID = e.Row.Cells[0].Text;
 SqlConnection con = ConnectionClass.GetConnection();
 SqlCommand cmd=new SqlCommand("delete from information where id='"+ Convert.ToInt32(autoID) + "'", con);
 cmd.ExecuteNonQuery();


hope it helps..
 
Share this answer
 

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