Click here to Skip to main content
15,891,677 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Error:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


C#
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
   {
       //int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["UserId"].ToString());
       string product = GridView1.DataKeys[e.RowIndex].Values["product"].ToString();

       con.Open();
       SqlCommand cmd = new SqlCommand("delete from shopping where product=" + product, con);
       int result = cmd.ExecuteNonQuery();
       con.Close();
       if (result == 1)
       {
           BindDetails();
           //lblresult.ForeColor = Color.Red;
           //lblresult.Text = username + " details deleted successfully";
       }

   }
Posted
Updated 20-Jul-12 1:47am
v2
Comments
[no name] 20-Jul-12 7:49am    
The error message is very clear. What is the issue?

Check the e.RowIndex value - it is probably negative to indicate no row.
 
Share this answer
 
Comments
shalinibharani 20-Jul-12 8:17am    
give me correct answer
OriginalGriff 20-Jul-12 8:23am    
1) What is the Magic Word?
2) Have you done as I said?
Espen Harlinn 20-Jul-12 9:58am    
That's probably it :-D
C#
string product = GridView1.DataKeys[e.RowIndex].Value;


The problem might be with the .Values["product"].

Check do you have datakey with the name "product".
 
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