This question already has an answer here:

I have 2 labels: errorLabel, where errors are stored lblUsernameGlobal, where Usernames are stored

Master Page File:

                <asp:LoginView runat="server" ViewStateMode="Disabled">
                    <AnonymousTemplate>
                        <ul class="nav navbar-nav navbar-right">
                            <asp:DropDownList ID="lstSprache" AutoPostBack="true" Visible="true" runat="server" >
                                <asp:ListItem>Deutsch</asp:ListItem>
                                <asp:ListItem>Berndeutsch</asp:ListItem>
                                <asp:ListItem>Englisch</asp:ListItem>
                            </asp:DropDownList>
                            <li><asp:Label ID = "lblUsernameGlobal" runat = "server" Text=""></asp:Label></li> {...}
            <asp:Label ID="errorLabel" runat="server" Text=""></asp:Label>
            <p>&copy; <%: DateTime.Now.Year %></p>
        </div>
    </div>
</form>

Login.aspx.cs:

protected void btnLogin_Click(object sender, EventArgs e)
    {
        username = txtBenutzername.Text;
        passwort = txtPasswort.Text;
        Label errorLabel = (Label)Master.FindControl("errorLabel");
        Label lblUsernameGlobal = (Label)Master.FindControl("lblUsernameGlobal");
        if (lblUsernameGlobal != null)
        {
            lblUsernameGlobal.Text = "BlaBla";
        }
        else
        {
            try
            {
                lblUsernameGlobal.Text = "BlaBla";
                errorLabel.Text = "Label nicht gefunden";
            }
            catch (Exception ex)
            {
                errorLabel.Text = ex.Message;
            }
        }
        if (username != "" && passwort != "")
        {
            //errorLabel.Text = dbConnect.SelectBenutzerSpecific(username, passwort);
            //lblUsernameGlobal.Text = username;
            //errorLabel.Text = username;
        }
        else
        {
            errorLabel.Text = "Invalid!";
        }
    }

After loads and loads of testing I have yet to find out, why the lblUsernameGlobal doesn't work. I can work with the errorlabel, but as soon as I try to reference the lblUsernameGlobal I get a NullReference. How can I reference lblUsernameGlobal without getting a NullReference?

Related posts

Recent Viewed