Link to home
Start Free TrialLog in
Avatar of zimmer9
zimmer9Flag for United States of America

asked on

How to resolve the error "Object reference not set to an instance of an object" when assigning a value to a label in ASP.NET?

I am developing an ASP.NET application using VS2010 and .NetFramework 4.0.

If I assign a value of "*" to the text box titled "txtdtCreated", I am successful.

However, if I assign a value to the label titled "lbldtCreated", I am getting the error:
"Object reference not set to an instance of an object".

Do you know how I can resolve this error?

 protected void grdUndelivUpd_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            dt = (DataTable)ViewState["grdUndelivUpd"];
            int selectedRowIndex = grdUndelivUpd.SelectedIndex;
            int sIndex = e.RowIndex;
                     
            GridViewRow row = grdUndelivUpd.Rows[sIndex];
                     
            TextBox ntxtdtCreated = grdUndelivUpd.Rows[sIndex].FindControl("txtdtCreated") as TextBox;
            Label nlbldtCreated = grdUndelivUpd.Rows[sIndex].FindControl("lbldtCreated") as Label;
            string sdtCreated = ntxtdtCreated.Text.ToString();
            if (sdtCreated.Length == 0)
            {
                //ntxtdtCreated.Text = "*";  Successful
                nlbldtCreated.Text = "*";    // error -> Object reference not set to an instance of an object
                lblStatus.Text = "Date is a required field. Enter format MM/DD/YYYY.";                
            }

---------------------------

<ItemStyle HorizontalAlign="Left"></ItemStyle>
                 </asp:TemplateField>                                                
                 <asp:TemplateField HeaderText="Created" ItemStyle-HorizontalAlign="Left">
                    <EditItemTemplate>                                        
                               <asp:TextBox ID="txtdtCreated" MaxLength="10" runat="server" Text='<%# Eval("dtCreated")  %>'></asp:TextBox>                    
                 <asp:CustomValidator ID="valDateRange" runat="server" ControlToValidate="txtdtCreated" Display="Dynamic"
                 Text="*"  ForeColor="Red" onservervalidate="valDateRange_ServerValidate" />              
                    </EditItemTemplate>                                          
                    <ItemTemplate>
                         <asp:Label ID="lbldtCreated" runat="server" Display="Dynamic" Text='<%# Eval("dtCreated") %>'></asp:Label>
                    </ItemTemplate>
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial