1

I am using asp.net LoginView to show different data to authenticated and anonymous users.

<asp:LoginView ID="LoginView1" Runat="server">
    <LoggedInTemplate>
        <asp:Label ID="Foo" runat="server" />
    </LoggedInTemplate>
    <AnonymousTemplate>
        <asp:Label ID="Bar" runat="server" />
    </AnonymousTemplate>
</asp:LoginView>

I then access these labels in my c# file like this:

Label Foo = (Label)LoginView1.FindControl("Foo");
Foo.Text = "whatever";

The error I am getting reads:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

3
  • Can you add your stack trace details as well? Commented Jun 27, 2013 at 19:59
  • It looks like it did not find your Label, try testing that Foo is not null before trying to use it. Commented Jun 27, 2013 at 20:01
  • 2
    Welcome to Stack Overflow! Almost all cases of NullReferenceException are the same. Please see "What is a NullReferenceException in .NET?" for some hints. Commented Jun 27, 2013 at 20:06

3 Answers 3

1

Well presumably at execution time, the user isn't logged in - so there's no control with an ID of Foo, so FindControl is returning null. You should either detect whether the user is logged in or not separately and ask for the right control, or check whether Foo is null before you use it. (You might want to consider renaming your local variable to foo to be more in tune with C# conventions, too.)

Sign up to request clarification or add additional context in comments.

1 Comment

how are you not a C# MVP for life? Really, Microsoft needs to bestow the lifetime achievement award in C# for you. :-)
0

Try this:

Label Foo = (Label)LoginView1.FindControl("Foo");
if(Foo != null)
{
    Foo.Text = "whatever";
}

Now you will not get the error, but if Foo is null, then your label's text will not update. You need to determine why it cannot find the Label named "Foo".

2 Comments

Isnt checking for null is strange here? How will there not be a control named Foo if you created one?
@AndersLindén - because there are two different templates; LoggedInTemplate and AnonymousTemplate, so there is a possibility that the Foo label is not there, because if the AnonymousTemplate is active, then the Bar label would exist and the Foo label would not.
0

You need to check for nulls:

object labelObj = LoginView1.FindControl("Foo")
if(labelObj != null)
{
    Label Foo = (Label)labelObj;
    if(Foo.Text!=null)
        Foo.Text="whatever";
}

1 Comment

Not very efficient. FindControl() result should be cached so you don't need to call it twice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.