Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<asp:TemplateField HeaderText="category" HeaderStyle-CssClass="gridview_header" ItemStyle-CssClass="gridview_text" ItemStyle-Width="100px">
    <itemtemplate>
        <asp:Label ID="lblcategory" runat="server" Text='<%#Eval("category") %>'>
        </asp:Label>
    </itemtemplate>
    <edititemtemplate>
        <asp:TextBox ID="txtcategory" runat="server" Text='<%#Eval("category") %>'>
        </asp:TextBox>
    </edititemtemplate>
</asp:TemplateField>


C#
protected void GvCategory_RowUpdating(object sender, GridViewUpdateEventArgs 
{
    Label category = (Label)GvCategory.Rows[e.RowIndex].FindControl("lblcategory");
    string categ=category.Text.ToString();
}


I am unable to retrieve the value of label lblcategory. I am getting an error Object reference not set to an instance of an object.

Please help me find the issue.

Thanks.
Posted
Updated 26-Jan-11 23:29pm
v3
Comments
Ankur\m/ 27-Jan-11 5:11am    
Please use 'PRE' tag to surround your code snippet.
TejuKrishna 27-Jan-11 5:15am    
those errors occurs when control id is some mis spelled or incorrect. pls check exact spelling of your label control id

Your problem is that you must find the control that is in the first cell, not in the row.

Label category = (Label)GvCategory.Rows[e.RowIndex].Cell[0].FindControl("lblcategory");
 
Share this answer
 
Comments
Sandeep Mewara 27-Jan-11 5:44am    
Comment from OP:
I used the above code but still getting the same error..
Ryan Zahra 27-Jan-11 5:52am    
Worked for me.
Please make sure that the cell index is pointing to the correct column. That is if the lblCategory is found in the first column, the cell index is 0, if the lblCategory is found in the second column, the cell index is 1 and so on.
hi,

use quickwatch to see value is comin in (Label)GvCategory.Rows[e.RowIndex].FindControl("lblcategory");

use #Bind("...")
 
Share this answer
 
<columns> <asp:boundfield datafield="category" headertext="Category" />
Use bound fields instead of templete field and
in code behind use as and give control as per your choice

Textbox txtcategory=(TextBox)GvCategory.Rows[e.RowIndex].Cells[0].Controls[0].

use it's working
 
Share this answer
 
v2
You are already in the edit mode, So you may not be able to access the text value of the label(i.e.Lblcategory). In order to retrieve the values of the textbox it is better to write the code in the Gridview_RowEditing( ) event.
 
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