0

I have got below error message:

Object reference not set to an instance of an object.

Code-behind:

public partial class Edit : System.Web.UI.Page
{
    private TextBox updated_time;

    protected void Page_Load(object sender, EventArgs e)
    {
        updated_time = (TextBox)ABC_DV.FindControl("txt_updated_time");
        updated_time.Text = DateTime.Now.ToString();
    }
}

how could i solve this ?

UPDATED

<asp:DetailsView ID="ABC_DV" runat="server" AutoGenerateRows="False"
        DefaultMode="Edit" DataKeyNames="TYPE_ID" DataSourceID="ABC_EDS">
        <Fields>
            <asp:TemplateField HeaderText="Type Id" SortExpression="TYPE_ID">
                <EditItemTemplate>
                    <asp:TextBox ID="txt_type_id" Width="200" runat="server" Text='<%# Bind("TYPE_ID") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("TYPE_ID") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("TYPE_ID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>        
            <asp:TemplateField HeaderText="Updated Time" SortExpression="UDPATED_TIME">
                <EditItemTemplate>
                    <asp:TextBox ID="txt_updated_time" Width="200" runat="server" Text='<%# Bind("UDPATED_TIME") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("UDPATED_TIME") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Labe2" runat="server" Text='<%# Bind("UDPATED_TIME") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>            
        </Fields>
    </asp:DetailsView>
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Bishan
  • 15,211
  • 52
  • 164
  • 258
  • 1
    Make sure that `ABC_DV` is properly instantiated and that `txt_updated_time` exists in `ABC_DV` so the `FindControl` call doesn't return null. – Quintin Robinson Nov 14 '12 at 05:50
  • At what level? Post your markup or relevant building code, `FindControl` is not recursive. – Quintin Robinson Nov 14 '12 at 05:52
  • Please post the markup un aspx – Carlos Landeras Nov 14 '12 at 05:56
  • @QuintinRobinson i have added aspx markup to the post – Bishan Nov 14 '12 at 06:02
  • @CarlosLande I have added aspx markup to the post – Bishan Nov 14 '12 at 06:02
  • 2
    @Bishan okay you need to take into consideration the mode the `DetailsView` is in when attempting to access a control, it *will not* exist in the hierarchy if it isn't in the edit mode causing the `Page_Load` to explode when it is called without the `DetailsView` in edit mode. Add some checks to your code to properly handle the control state. – Quintin Robinson Nov 14 '12 at 06:05
  • 1
    Almost all cases of NullReferenceException are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Nov 14 '12 at 06:21
  • Seems like `(TextBox)ABC_DV.FindControl("txt_updated_time");` is returning null, which means `ABC_DV.FindControl()` cannot find `txt_updated_time`. Can you upload the aspx code of ABC_DV? – Arian Motamedi Nov 14 '12 at 05:53
  • i have added aspx markup to the post – Bishan Nov 14 '12 at 06:02
  • Your textbox does have the correct ID, so that's not the issue. The only thing I can think of is that the control doesn't exist at the time `ABC_DV.FindControl` is being executed, like what others suggested. – Arian Motamedi Nov 14 '12 at 06:12

3 Answers3

1

Okay you need to take into consideration the mode the DetailsView is in when attempting to access a control, it will not exist in the hierarchy if it isn't in the edit mode causing the Page_Load to explode when it is called without the DetailsView in edit mode. Add some checks to your code to properly handle the control state.

protected void Page_Load(object sender, EventArgs e)
{
    if (ABC_DV.CurrentMode == DetailsViewMode.Edit) {
      updated_time = (TextBox)ABC_DV.FindControl("txt_updated_time");
      if(null != updated_time)
        updated_time.Text = DateTime.Now.ToString();
    }
}
Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
0
TextBox updated_time = ABC_DV.FindControl("txt_updated_time") as TextBox;
if (updated_time  != null)
{
    updated_time.Text = DateTime.Now.ToString();
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
0

By the time you hit the Page_Load method your controls should have been reconstructed and added back into the page. The fact that you are getting an error at that point indicates that particular control doesn't exist - at least not with the ID that you've specified.

Try moving the code into your PreRender() - this is the method that is executed just before the page is rendered to the response stream, if you have added dynamic controls or messed with the IDs of controls then that should have happened well before this stage.

slugster
  • 49,403
  • 14
  • 95
  • 145