Saturday, March 24, 2012

Object reference not set to an instance of an object

Hi,
I want to access in code-behind a label within the ItemTemplate of a
Formview.
<asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource1" >
<ItemTemplate>
<asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
%>'></asp:Label>
code-behind:
Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text
I get the error: "Object reference not set to an instance of an object"
Could somebody tell me what's wrong in my code?
Thanks
BenHi Ben,
The direct answer is: nothing wrong.
It's probably an issue outside given code. Checks FormView1.Row and FormView
1.DataItem
are not null. If yes then be sure you have at least one record in FormView1'
s
source and DataBind is performed on the FormView1. You cannot retrieve contr
ols
if they are not processed.
Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com
B> FormView1.FindControl("nameLabel")
B>

0 comments:

Post a Comment