zimmer9
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["grdU ndelivUpd" ];
int selectedRowIndex = grdUndelivUpd.SelectedInde x;
int sIndex = e.RowIndex;
GridViewRow row = grdUndelivUpd.Rows[sIndex] ;
TextBox ntxtdtCreated = grdUndelivUpd.Rows[sIndex] .FindContr ol("txtdtC reated") as TextBox;
Label nlbldtCreated = grdUndelivUpd.Rows[sIndex] .FindContr ol("lbldtC reated") as Label;
string sdtCreated = ntxtdtCreated.Text.ToStrin g();
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"></I temStyle>
</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="txtdtCr eated" Display="Dynamic"
Text="*" ForeColor="Red" onservervalidate="valDateR ange_Serve rValidate" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbldtCreated" runat="server" Display="Dynamic" Text='<%# Eval("dtCreated") %>'></asp:Label>
</ItemTemplate>
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(
{
dt = (DataTable)ViewState["grdU
int selectedRowIndex = grdUndelivUpd.SelectedInde
int sIndex = e.RowIndex;
GridViewRow row = grdUndelivUpd.Rows[sIndex]
TextBox ntxtdtCreated = grdUndelivUpd.Rows[sIndex]
Label nlbldtCreated = grdUndelivUpd.Rows[sIndex]
string sdtCreated = ntxtdtCreated.Text.ToStrin
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"></I
</asp:TemplateField>
<asp:TemplateField HeaderText="Created" ItemStyle-HorizontalAlign=
<EditItemTemplate>
<asp:TextBox ID="txtdtCreated" MaxLength="10" runat="server" Text='<%# Eval("dtCreated") %>'></asp:TextBox>
<asp:CustomValidator ID="valDateRange" runat="server" ControlToValidate="txtdtCr
Text="*" ForeColor="Red" onservervalidate="valDateR
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbldtCreated" runat="server" Display="Dynamic" Text='<%# Eval("dtCreated") %>'></asp:Label>
</ItemTemplate>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.