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
    • Loading...
    • tgopi99
    • Joined on 09-28-2007, 12:14 AM
    • Posts 65

    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
    • Loading...
    • azamsharp
    • Joined on 06-11-2003, 9:36 AM
    • Houston,Texas
    • Posts 4,306

     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;

  • Re: Check null value for an object

    10-25-2007, 9:37 PM
    • Loading...
    • mbanavige
    • Joined on 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 7,702
    • Moderator
      TrustedFriends-MVPs

    like this:

    TextBox tbox = (TextBox)Page.PreviousPage.FindControl("textboxFirstName");
    if (tbox == null) {
        // it was null
    }
     
    Mike Banavige
    ~~~~~~~~~~~~
    Dont 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 so we will all know you have been helped.
  • Re: Check null value for an object

    10-26-2007, 1:38 AM
    • Loading...
    • ravipahuja1
    • Joined on 06-27-2007, 6:58 AM
    • Delhi NCR
    • Posts 627

     Go for "as" as suggest by azam.

  • Re: Check null value for an object

    10-26-2007, 2:35 AM
    • Loading...
    • ramana123
    • Joined on 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
    • Loading...
    • impathan
    • Joined on 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
    • Loading...
    • KinjanShah
    • Joined on 10-16-2007, 3:43 AM
    • Ahmedabad, Gujarat, India
    • Posts 42
    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
    ----------------------------------------------------
    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
    • Loading...
    • tgopi99
    • Joined on 09-28-2007, 12:14 AM
    • Posts 65

    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
    • Loading...
    • tgopi99
    • Joined on 09-28-2007, 12:14 AM
    • Posts 65

    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
    • Loading...
    • azamsharp
    • Joined on 06-11-2003, 9:36 AM
    • Houston,Texas
    • Posts 4,306

     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.

  • Re: Check null value for an object

    10-26-2007, 4:09 PM
    Answer
    • Loading...
    • azamsharp
    • Joined on 06-11-2003, 9:36 AM
    • Houston,Texas
    • Posts 4,306

     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"  %>
     

     

  • Re: Check null value for an object

    10-26-2007, 4:49 PM
    • Loading...
    • tgopi99
    • Joined on 09-28-2007, 12:14 AM
    • Posts 65

    Very useful one, Thank you very much.

  • Re: Check null value for an object

    10-29-2007, 7:26 AM

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