I have encountered this error "Object reference not set to an instance of an object." when i trying to set a value which i had session from the previous page and set it on a template field. when I debugged it, the session value was there(successfully session over). what i am trying to do is to display the value of what I had session on detailsview.
aspx :
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False">
<Fields>
<asp:TemplateField HeaderText="Order Number " SortExpression="poNum">
<ItemTemplate>
<asp:Label ID="test" runat="server" Text='<%# Bind("poNum") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
code behind :
protected void Page_Load(object sender, EventArgs e)
{
((Label)DetailsView1.FindControl("test")).Text = Session["poNum"].ToString();
}
I was wondering why I was unable to get the control from detailsview??
EDIT
Label aaa = new Label();
aaa.Text = Session["poNum"].ToString();
Label orderNum = (Label)DetailsView1.FindControl("test"); // orderNum was null here
orderNum.Text = aaa.Text;
You can access the Label in DataBound event of DetailsView.