GridView returning null when I try to grab the GridView to display data
<p>I am trying to return the table of Users and display it on a page which uses a masterpage.</p>

<p>My master page file called AjaxMaster.Master looks like this - </p>

<pre><code>            &lt;%@ Master Language="C#" AutoEventWireup="true" CodeBehind="AjaxMaster.master.cs"
                Inherits="e.AjaxMaster" %&gt;

            &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
            &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
            &lt;head id="AjaxHead1" runat="server"&gt;
                &lt;title&gt;&lt;/title&gt;

                &lt;link rel="stylesheet" type="text/css" href="Global.css" /&gt;
                &lt;style type="text/css"&gt;
                    #backtoblog a
                    {
                        position: absolute;
                        top: 7px;
                        left: 15px;
                        text-decoration: none;
                    }
                &lt;/style&gt;
                &lt;asp:ContentPlaceHolder ID="ajaxhead" runat="server"&gt;
                &lt;/asp:ContentPlaceHolder&gt;
            &lt;/head&gt;
            &lt;body&gt;
                &lt;form id="ajaxform1" runat="server"&gt;

                &lt;asp:ToolkitScriptManager ID="AjaxToolkitScriptManager2" runat="server" /&gt;
                &lt;div&gt;
                &lt;/div&gt;
                &lt;asp:LoginView ID="AjaxLoginView1" runat="server"&gt;
                    &lt;LoggedInTemplate&gt;
                        &lt;div id="companyLogo"&gt;
                            &lt;img src="images/company_logo.png" alt="Company Logo" border="0" width="123" border="0"
                                height="40" /&gt;
                        &lt;/div&gt;
                        &lt;div id="header"&gt;
                            &lt;!-- start of main --&gt;

                            &lt;div id="welcome"&gt;
                                &lt;a href="Welcome.aspx"&gt;Welcome&lt;/a&gt; &lt;strong&gt;
                                    &lt;asp:LoginName ID="LoginName1" runat="server" /&gt;
                                &lt;/strong&gt;, [
                                &lt;asp:LoginStatus ID="LoginStatus1" runat="server" LogoutAction="RedirectToLoginPage" /&gt;
                                ]
                            &lt;/div&gt;
                            &lt;span id='sm_holder'&gt;&lt;/span&gt;
                            &lt;div class="clear"&gt;
                            &lt;/div&gt;
                            &lt;%

                                string sPagePath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
                                System.IO.FileInfo oFileInfo = new System.IO.FileInfo(sPagePath);
                                string sPageName = oFileInfo.Name;
                            %&gt;

                            &lt;div&gt;
                                &lt;!-- start of TabContainer --&gt;
                                &lt;asp:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="1"&gt;
                                    &lt;asp:TabPanel runat="server" HeaderText="Home" ID="TabPanel1"&gt;
                                        &lt;HeaderTemplate&gt;
                                            &lt;span&gt;
                                                &lt;img src="iconimages/home001.png" alt="Home" border="0" height="10px" /&gt;&amp;nbsp;Home&lt;/span&gt;
                                        &lt;/HeaderTemplate&gt;
                                        &lt;ContentTemplate&gt;
                                            &lt;!-- Start of Home --&gt;
                                            &lt;% Response.WriteFile("HomeTab.aspx");  %&gt;
                                            &lt;!-- End of Home --&gt;
                                        &lt;/ContentTemplate&gt;
                                    &lt;/asp:TabPanel&gt;
                                    &lt;asp:TabPanel runat="server" HeaderText="Dashboard" ID="TabPanel7"&gt;
                                        &lt;HeaderTemplate&gt;
                                            &lt;p&gt;
                                                Dashboard&lt;/p&gt;
                                        &lt;/HeaderTemplate&gt;
                                        &lt;ContentTemplate&gt;
                                            &lt;!-- Start of Dashboard --&gt;
                                            &lt;% Response.WriteFile("DashboardTab.aspx");  %&gt;
                                            &lt;!-- End of Dashboard --&gt;
                                        &lt;/ContentTemplate&gt;
                                    &lt;/asp:TabPanel&gt;
                                    &lt;asp:TabPanel ID="TabPanel2" runat="server" HeaderText="Locations"&gt;
                                        &lt;HeaderTemplate&gt;
                                            &lt;p&gt;
                                                Locations&lt;/p&gt;
                                        &lt;/HeaderTemplate&gt;
                                        &lt;ContentTemplate&gt;
                                            &lt;!-- Start of Locations --&gt;
                                            &lt;% Response.WriteFile("LocationsTab.aspx");  %&gt;
                                            &lt;!-- End of Locations --&gt;
                                        &lt;/ContentTemplate&gt;
                                    &lt;/asp:TabPanel&gt;
                                    &lt;asp:TabPanel ID="TabPanel3" runat="server" HeaderText="Users"&gt;
                                        &lt;HeaderTemplate&gt;
                                            &lt;p&gt;
                                                Users&lt;/p&gt;
                                        &lt;/HeaderTemplate&gt;
                                        &lt;ContentTemplate&gt;
                                            &lt;!-- Start of Users --&gt;
                                            &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="&lt;%$ ConnectionStrings: SiteSQL %&gt;"
                                                SelectCommand="SELECT * FROM aspnet_Users"&gt;&lt;/asp:SqlDataSource&gt;
                                            &lt;asp:GridView ID="GridView1" runat="server"&gt;
                                            &lt;/asp:GridView&gt;
                                            &lt;!-- End of Users --&gt;
                                        &lt;/ContentTemplate&gt;
                                    &lt;/asp:TabPanel&gt;
                                    &lt;asp:TabPanel ID="TabPanel4" runat="server" HeaderText="Reports"&gt;
                                        &lt;HeaderTemplate&gt;
                                            &lt;p&gt;
                                                Reports&lt;/p&gt;
                                        &lt;/HeaderTemplate&gt;
                                        &lt;ContentTemplate&gt;
                                            &lt;!-- Start of Reports --&gt;
                                            &lt;% Response.WriteFile("ReportsTab.aspx");  %&gt;
                                            &lt;!-- End of Reports --&gt;
                                        &lt;/ContentTemplate&gt;
                                    &lt;/asp:TabPanel&gt;
                                    &lt;asp:TabPanel ID="TabPanel5" runat="server" HeaderText="Alerts"&gt;
                                        &lt;HeaderTemplate&gt;
                                            &lt;p&gt;
                                                Alerts&lt;/p&gt;
                                        &lt;/HeaderTemplate&gt;
                                        &lt;ContentTemplate&gt;

                                        &lt;/ContentTemplate&gt;
                                    &lt;/asp:TabPanel&gt;
                                    &lt;asp:TabPanel ID="TabPanel6" runat="server" HeaderText="Preferences"&gt;
                                        &lt;HeaderTemplate&gt;
                                            &lt;p&gt;
                                                Preferences&lt;/p&gt;
                                        &lt;/HeaderTemplate&gt;
                                        &lt;ContentTemplate&gt;
                                            &lt;!-- Start of Preferences --&gt;
                                            &lt;!-- start of left Panel for Preferences --&gt;
                                            &lt;%-- &lt;div id="leftColumn"&gt;--%&gt;

                                            &lt;%--   &lt;/div&gt;--%&gt;
                                            &lt;!-- end of left Panel for Preferences --&gt;
                                            &lt;!-- End of Preferences --&gt;
                                        &lt;/ContentTemplate&gt;
                                    &lt;/asp:TabPanel&gt;
                                &lt;/asp:TabContainer&gt;
                                &lt;!-- end of TabContainer --&gt;

                                &lt;div class="clear"&gt;
                                &lt;/div&gt;
                                &lt;div class="line"&gt;
                                &lt;/div&gt;
                                &lt;div&gt;
                                    &lt;asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"&gt;
                                    &lt;/asp:ContentPlaceHolder&gt;
                                &lt;/div&gt;

                                &lt;div id="footer"&gt;

                                &lt;/div&gt;
                    &lt;/LoggedInTemplate&gt;
                    &lt;AnonymousTemplate&gt;
                        You are not logged in.
                        &lt;br /&gt;
                        Please login to access eservice
                    &lt;/AnonymousTemplate&gt;
                &lt;/asp:LoginView&gt;
                &lt;/form&gt;
            &lt;/body&gt;
            &lt;/html&gt;
</code></pre>

<p>And the code-behind file which looks like this - </p>

<pre><code>            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Web;
            using System.Web.UI;
            using System.Web.UI.WebControls;
            using System.Data.SqlClient;

            namespace e
            {
                public partial class AjaxMaster : System.Web.UI.MasterPage
                {
                    protected void Page_Load(object sender, EventArgs e)
                    {



                        if (!IsPostBack)
                        {
                            BindUsersGrid();
                        }
                    }


                    private void BindUsersGrid()
                    {
                        SqlConnection conn;
                        SqlCommand comm;

                        SqlDataReader reader;

                        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SiteSQL"].ConnectionString;

                        conn = new SqlConnection(connectionString);


                        comm = new SqlCommand("SELECT * FROM aspnet_Users", conn);

                        try
                        {
                            conn.Open();
                            reader = comm.ExecuteReader();

                            GridView gridviewusers = (GridView)AjaxLoginView1.FindControl("GridView1");



                            gridviewusers.DataSource = reader;

                            gridviewusers.DataBind();


                            reader.Close();


                        }

                        finally
                        {
                            conn.Close();
                        }


                    }


                }
            }
</code></pre>

<p>It compiles ok but when I run it, the line - </p>

<pre><code>GridView gridviewusers = (GridView)AjaxLoginView1.FindControl("GridView1");
</code></pre>

<p>returns null and the following lines throw an exception - </p>

<pre><code>            gridviewusers.DataSource = reader;

            gridviewusers.DataBind();
</code></pre>

<p>The stack trace of the exception looks like this - </p>

<pre><code>             Object reference not set to an instance of an object.
            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.

            Source Error:

            An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

            Stack Trace:

            [NullReferenceException: Object reference not set to an instance of an object.]
               eservice.AjaxMaster.BindUsersGrid() in AjaxMaster.Master.cs:115
               eservice.AjaxMaster.Page_Load(Object sender, EventArgs e) in AjaxMaster.Master.cs:27
               System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
               System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
               System.Web.UI.Control.OnLoad(EventArgs e) +99
               System.Web.UI.Control.LoadRecursive() +50
               System.Web.UI.Control.LoadRecursive() +141
               System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627


            Version Information: Microsoft .NET Framework Version:2.0.50727.3603; ASP.NET Version:2.0.50727.3614 
</code></pre>

<p>Line 115 of AjaxMaster.Master.cs is - </p>

<pre><code>gridviewusers.DataSource = reader;
</code></pre>
