Accessing control in a gridview
<p>I'm attempting to access controls in a gridview with no luck. I've searched the internet and no examples have been able to help me. Currently, the error message I'm getting is: "Object reference not set to an instance of an object" referring to this line: lblErrors.Text = level.SelectedValue;</p>

<p>Here is the my code behind:</p>

<pre><code>protected void setRecruitmentSchoolLevel(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        GridView recruitment = (GridView)sender;
        DropDownList level = (DropDownList)e.Row.FindControl("lstLevel");
        lblErrors.Text = level.SelectedValue;
    }
}
</code></pre>

<p>Here is my website code:</p>

<pre><code>                        &lt;asp:GridView ID="gridRecruitment" runat="server" DataSourceID="sourceRecruitment" Width="100%"
                        CellPadding="4" EnableModelValidation="True" ForeColor="#333333" OnRowDataBound="setRecruitmentSchoolLevel" 
                        GridLines="None" AutoGenerateColumns="False" AllowSorting="true" DataKeyNames="ID" EmptyDataText="No Recruitments"&gt;
                        &lt;Columns&gt;
                            &lt;asp:HyperLinkField HeaderText="ID" DataTextField="ID" DataNavigateUrlFormatString="~/Admin/Recruit/recruit.aspx?ID={0}" DataNavigateURLFields="ID" Target="_blank" SortExpression="ID"/&gt;
                            &lt;asp:BoundField HeaderText="Class" DataField="GraduationYear" SortExpression="GraduationYear"/&gt;
                            &lt;asp:TemplateField HeaderText="Level" SortExpression="LevelName"&gt;
                                &lt;ItemTemplate&gt;&lt;%#Eval("LevelName") %&gt;&lt;/ItemTemplate&gt;                                        
                                &lt;EditItemTemplate&gt;
                                    &lt;asp:DropDownList ID="lstLevel" runat="server" DataSourceID="sourceLevels" DataTextField="Abbreviation" DataValueField="ID" SelectedValue='&lt;%#Bind("Level") %&gt;'/&gt;
                                    &lt;asp:SqlDataSource ID="sourceLevels" runat="server" ConnectionString="&lt;%$ ConnectionStrings:DBCS %&gt;" SelectCommand="SELECT Abbreviation, ID FROM Levels WHERE Recruiting = 'True' ORDER BY [Order] DESC"/&gt;                                        
                                &lt;/EditItemTemplate&gt;
                            &lt;/asp:TemplateField&gt;
                            &lt;asp:CommandField HeaderText="Commands" ShowEditButton="true" ShowDeleteButton="true" /&gt;
                        &lt;/Columns&gt;
                    &lt;/asp:GridView&gt;
</code></pre>
