Click here to Skip to main content
15,917,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm trying to delete row from gridview but there is an execption coming and cant understand what is the problem.
C#
if (e.CommandName.Equals("Delete"))
{
    int index = Convert.ToInt32(e.CommandArgument);
    var id = ((Label)GridView1.Rows[index].Cells[0].FindControl("lblid")).Text.Trim(); // execption on this line

    SqlConnection con = new SqlConnection(connstring);
    string qry = "Delete from ['ISB VAS Nodes$'] where ID = " + id + "";
    SqlCommand com = new SqlCommand(qry, con);

    // open connection
    con.Open();
    int numberDeleted = com.ExecuteNonQuery();

    if (numberDeleted > 0)
    {
        Label2.Text = "Deleted";
        binddata();
    }
    else
    {
        Label2.Text = "Not Deleted";
    }

    // close connection
    con.Close();
}
Posted
Updated 23-Apr-13 21:13pm
v2
Comments
Mohan_V 24-Apr-13 3:10am    
Can you give the exception
_Amy 24-Apr-13 3:15am    
What exception you are getting?
Rockstar_ 24-Apr-13 3:16am    
var id = ((Label)GridView1.Rows[index].FindControl("lblid")).Text.Trim();

Try this code...
fak_farrukh 24-Apr-13 3:48am    
Object reference not set to an instance of an object.
execption is coming after adding this code
Rockstar_ 24-Apr-13 6:01am    
GridviewRow grow=GridView1.Rows[0];
grow.findcontrol();

Use something like...

 
Share this answer
 
Get the GridViewRow through the parent of your command source and then try finding the label.
Try this:
C#
GrieViewRow row = (GridViewRow)((LinkButton)e.CommandSource).Parent.Parent;
var id = ((Label)row.FindControl("lblid")).Text.Trim();



--Amit
 
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