all groups > vb.net controls > june 2007 >
You're in the

vb.net controls

group:

Object reference not set to an instance of an object


Object reference not set to an instance of an object Ben
6/26/2007 12:00:00 AM
vb.net controls:
Hi,

(i tired to post it in newsgroup asp.net but it never came into ).

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
Ben




Re: Object reference not set to an instance of an object Tom John
7/5/2007 9:32:59 PM
Ben

You need to access the DataItem of the FormView once the control has
been databound to access the currently bound object:

Private Sub FormView1_DataBound(ByVal sender As Object, ByVal e As
EventArgs) Handles FormView1.DataBound

Dim rowView As DataRowView = CType(FormView1.DataItem,
DataRowView)
Dim name As String = rowView("Text").ToString()
Response.Write(name)

End Sub

There's a good example on MSDN:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.dataitem.aspx

Hope this helps

Tom


[quoted text, click to view]
AddThis Social Bookmark Button