Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi , how do i solved this problem?
i want to delete specific row in my gridview and i using btn in the item templates to delete.
here my btn onlick codes and my gridview_rowcommand
C#
protected void Button1_Click(object sender, EventArgs e)
{
    GridViewRow gRow = (GridViewRow)((Button)(sender)).NamingContainer;

    // this line is the one with Object reference not set to an instance of an object error
    string strId =((HiddenField)gRow.FindControl("shoppingCartID")).Value;
}


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    GridViewRow gRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
    int RowIndex = gRow.RowIndex;
}
Posted
Updated 17-Jun-13 22:46pm
v2

This error happens when you try to use a property or call a method of an object that is null. You are trying to access an object of GridViewRow gRow, which is null. Try to step through and debug your source code. Try this:
C#
GridViewRow gRow = (GridViewRow)((Button)sender).NamingContainer;

OR
C#
GridViewRow grdRow = (GridViewRow)((Button)sender).Parent.Parent;


--Amit
 
Share this answer
 
Comments
NooobieCoder 18-Jun-13 5:04am    
it is still the same.
NooobieCoder 18-Jun-13 5:06am    
i have tried this and get a new problem instead.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
GridViewRow gRow = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int RowIndex = gRow.RowIndex;
}
}

i get this problem :

ObjectDataSource 'ObjectDataSource' could not find a non-generic method 'Delete' that has parameters
_Amy 18-Jun-13 5:09am    
maybe you have bound column instead of button. Can I see your HTML?
NooobieCoder 18-Jun-13 5:11am    
i add a button in the item template in gridview which i wan to use. Html of which one?
_Amy 18-Jun-13 5:17am    
Then check for shoppingCartID, whether it exists in itemtemplate or not.
Since you want to perform CRUD operations using objectdatasource, this[^] is a very nice article to get started.
 
Share this answer
 
v2
Comments
NooobieCoder 18-Jun-13 5:41am    
how to write the deletion method?sry I am very bad CRUD especially with gridview
Zafar Sultan 18-Jun-13 5:45am    
It's all right. Once you get till here(obtaining the id of record to be deleted) it's not that difficult. All you have to do is to call SP that deletes the record based on its ID passed as parameter.
NooobieCoder 18-Jun-13 5:42am    
bad at*
Zafar Sultan 18-Jun-13 5:48am    
You can find a very good article about CRUD operations at http://www.dotnetfunda.com/articles/article1619-how-to-perform-edit-update-and-delete-operation-in-gridview.aspx
NooobieCoder 18-Jun-13 5:48am    
SP?

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