Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
               int task_id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);//this Line Gives Error
        using (DailyWorkReportDBEntities1 context = new DailyWorkReportDBEntities1())
        {
            Task_Mst obj = context.Task_Mst.First(x => x.Task_Id == task_id);
            context.Task_Mst.DeleteObject(obj);
            context.SaveChanges();
            FillTask();
            ClientScript.RegisterStartupScript(this.GetType(), "alertMessage", "alert('Data Deleted');", true);
        }
    }


Thanks in Advance
Posted
Updated 30-Jun-15 23:50pm
v2

Start by looking at the value of e.RowIndex and see exactly what it is: there is a good chance that it's negative, indicating there are no rows.

But without knowing exactly when this is occurring, and how many rows you have, which one is being deleted, and what the values are, we can't help. And that means running your app, with your data, and your user inputs - which we can't do!

So use the debugger, and start looking at exactly when and what is happening.
Sorry - but we can't do that for you!
 
Share this answer
 
use single instead of first it will stop the run time exception and check if the data exists for the given lambda expression in the query
 
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