Check null value for an object

Last post 10-29-2007 7:26 AM by sandeepthekka. 12 replies.

Sort Posts:

  • Check null value for an object

    10-25-2007, 9:24 PM
    • Member
      6 point Member
    • tgopi99
    • Member since 09-28-2007, 12:14 AM
    • Posts 82

    Hi,

          How to check the object is null in C#. For the code below

    TextBox tbox = new TextBox();

    tbox = (TextBox)Page.PreviousPage.FindControl("textboxFirstName");

    i want to find whether the tbox is null or not. How can i achieve this in C#.

    Please help.

  • Re: Check null value for an object

    10-25-2007, 9:28 PM
    • All-Star
      23,564 point All-Star
    • azamsharp
    • Member since 06-11-2003, 9:36 AM
    • Houston,Texas
    • Posts 4,513
    • TrustedFriends-MVPs

     You can use the "as" keyword to try to cast the object. If the casting fails null is returned.

     

    tbox =Page.PreviousPage.FindControl("textboxFirstName") as TextBox;

    HighOnCoding
    Get high on ASP.NET articles, videos, podcasts and more!
  • Re: Check null value for an object

    10-25-2007, 9:37 PM
    • All-Star
      96,247 point All-Star
    • mbanavige
    • Member since 11-06-2003, 8:29 AM
    • New England, USA
    • Posts 10,198
    • Moderator
      TrustedFriends-MVPs

    like this:

    TextBox tbox = (TextBox)Page.PreviousPage.FindControl("textboxFirstName");
    if (tbox == null) {
        // it was null
    }
     
    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: Check null value for an object

    10-26-2007, 1:38 AM
    • Contributor
      3,557 point Contributor
    • ravipahuja1
    • Member since 06-27-2007, 6:58 AM
    • Delhi NCR
    • Posts 629

     Go for "as" as suggest by azam.

  • Re: Check null value for an object

    10-26-2007, 2:35 AM
    • Participant
      838 point Participant
    • ramana123
    • Member since 06-27-2005, 12:02 PM
    • Bangalore
    • Posts 224

    Use

     

    if(tbox!=null)

    {

    //do your code if control finds.

    }

    else

    {

    //tbox is null/

    }

    Cheers
    Ram MCP
  • Re: Check null value for an object

    10-26-2007, 2:56 AM
    • Contributor
      4,243 point Contributor
    • impathan
    • Member since 07-12-2007, 10:12 AM
    • Ahmedabad
    • Posts 787

     

    tgopi99:
    tbox = (TextBox)Page.PreviousPage.FindControl("textboxFirstName");

    if( tbox!=null)

    {

    }

     

    best rEgard

    pAthan 

    Click on 'Mark as Answer' if this post is helpful.

    ImranKhan pathan
  • Re: Check null value for an object

    10-26-2007, 3:10 AM
    • Member
      533 point Member
    • KinjanShah
    • Member since 10-16-2007, 3:43 AM
    • Ahmedabad, Gujarat, India
    • Posts 103
    Hi tgopi99, In Order to check for object Reference in C#.NET, you need to use null, in VB.NET you need to use Nothing. if(tbox == null) { //code for nothing } else { //Code for presence } Hope this will help you out. Thanks and Regards, Kinjan Shah, MCAD
    Thanks and Regards,
    Kinjan Shah,
    MCAD
    http://TricksForProgramming.BlogSpot.com
    ----------------------------------------------------
    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved.
  • Re: Check null value for an object

    10-26-2007, 1:48 PM
    • Member
      6 point Member
    • tgopi99
    • Member since 09-28-2007, 12:14 AM
    • Posts 82

    Hi,

       I am getting the following error when i am checking null value of an object as you said. Here is my code

    tbox = (TextBox)Page.PreviousPage.FindControl("textboxFirstName") as TextBox;if (tbox != null)

    {

    Session[
    "semfname"] = tbox.Text;

    txtfirstname.Text = tbox.Text;

    }

     

    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:

    Line 39:                 TextBox tbox = new TextBox();
    Line 40:                 DropDownList dlist = new DropDownList();
    Line 41:                 tbox = (TextBox)Page.PreviousPage.FindControl("textboxFirstName") as TextBox;
    Line 42:                 if (tbox != null)
    Line 43:                 {

  • Re: Check null value for an object

    10-26-2007, 1:49 PM
    • Member
      6 point Member
    • tgopi99
    • Member since 09-28-2007, 12:14 AM
    • Posts 82

    I even checked with the above code, but still it is giving the following error.

     

    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:

    Line 39:                 TextBox tbox = new TextBox();
    Line 40:                 DropDownList dlist = new DropDownList();
    Line 41:                 tbox = (TextBox)Page.PreviousPage.FindControl("textboxFirstName") as TextBox;
    Line 42:                 if (tbox != null)
    Line 43:                 {

     

  • Re: Check null value for an object

    10-26-2007, 3:52 PM
    • All-Star
      23,564 point All-Star
    • azamsharp
    • Member since 06-11-2003, 9:36 AM
    • Houston,Texas
    • Posts 4,513
    • TrustedFriends-MVPs

     First check that the PreviousPage property is not null and also make sure that the Previous Page consists of the element named textboxFirstName. You can also expose properties in the PreviousPage to access the controls instead of using FindControl method.

    HighOnCoding
    Get high on ASP.NET articles, videos, podcasts and more!
  • Re: Check null value for an object

    10-26-2007, 4:09 PM
    Answer
    • All-Star
      23,564 point All-Star
    • azamsharp
    • Member since 06-11-2003, 9:36 AM
    • Houston,Texas
    • Posts 4,513
    • TrustedFriends-MVPs

     Here is a small example that will clear up the things further:

    This is the code for my previousPage:

      <asp:TextBox ID="txtName" runat="server" />
            <asp:Button ID="Button1" runat="server" PostBackUrl="~/FirstPage.aspx" Text="Button" OnClick="Button1_Click" />

     

    And here is the page that access the PreviousPage.

     protected void Page_Load(object sender, EventArgs e)
            {
                if (PreviousPage != null)
                    {
                        TextBox tb = PreviousPage.FindControl("txtName") as TextBox;
                        Response.Write(tb.Text);
                    }
               
                if (!Page.IsPostBack)
                {
                               
                }
            }

    If you are not able to find the control in the PreviousPage then you can use properties in the Previous to access the value of the control. Like the following:

    public class MyPreviousPage : ....

    public string Name { return txtName.Text; }

    Now, in the main page you can access like this:

    MyPreviousPage.Name

    You will also need to add the following directive in the main page (NOT Previous Page)

    <%@ PreviousPageType VirtualPath="~/MyPreviousPage.aspx"  %>
     

     

    HighOnCoding
    Get high on ASP.NET articles, videos, podcasts and more!
  • Re: Check null value for an object

    10-26-2007, 4:49 PM
    • Member
      6 point Member
    • tgopi99
    • Member since 09-28-2007, 12:14 AM
    • Posts 82

    Very useful one, Thank you very much.

  • Re: Check null value for an object

    10-29-2007, 7:26 AM
    • Member
      562 point Member
    • sandeepthekka
    • Member since 10-23-2007, 2:53 AM
    • kerala
    • Posts 163

    memberdetails.OfficePhone = txtOfficePhone.Text == "" ? "" : txtOfficePhone.Text; memberdetails.ResidencePhone = txtResPhone.Text == "" ? "" : txtResPhone.Text; memberdetails.Mobile = txtMobile.Text == "" ? "" : txtMobile.Text;
Page 1 of 1 (13 items)