locked
To Check Null value RRS feed

  • Question

  • Hi

    In c# I have to check the null value

              
    TextBox branchName = (this.Controls.Find(txtBoxTwo, true).First() as TextBox);

    If  it is Null it shows

    "Key specified  was either empty or null Parameter name:Key".How to fix the error.

    Please help me.

    Thanks

    Ram

    Monday, December 31, 2012 7:02 AM

Answers

  • Hi,
    i assume you are using ASP.NET .
    This may help :

    object obj = this.FindControl("textBoxTow");
                    TextBox tb = null;
                    string message = string.Empty;
                    if (obj == null)
                    {
                        message = "Key specified  was either empty or null Parameter name:Key";
                    }
                    else
                    {
                        tb = (obj as TextBox);
                    }


    One good question is equivalent to ten best answers.

    • Proposed as answer by Hirendra Sisodiya Monday, December 31, 2012 8:53 AM
    • Marked as answer by Lisa Zhu Friday, January 4, 2013 8:06 AM
    Monday, December 31, 2012 7:13 AM
  • The exception message you are seeing occurs if the first parameter 'txtBoxTwo' that you are passing to Find is either NULL or Empty. So, check whether txtBoxTwo is NULL or Empty and assign valid value. Anyways, I assume that the value of txtBoxTwo is "txtBoxTwo" and updated your code like below,

    string txtBoxTwo = "txtBoxTwo";
    var controls = this.Controls.Find(txtBoxTwo, true); TextBox branchName = controls.Length > 0 ? controls.First() as TextBox : null;

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by Lisa Zhu Friday, January 4, 2013 8:06 AM
    Monday, December 31, 2012 7:32 AM

All replies

  • Hi,
    i assume you are using ASP.NET .
    This may help :

    object obj = this.FindControl("textBoxTow");
                    TextBox tb = null;
                    string message = string.Empty;
                    if (obj == null)
                    {
                        message = "Key specified  was either empty or null Parameter name:Key";
                    }
                    else
                    {
                        tb = (obj as TextBox);
                    }


    One good question is equivalent to ten best answers.

    • Proposed as answer by Hirendra Sisodiya Monday, December 31, 2012 8:53 AM
    • Marked as answer by Lisa Zhu Friday, January 4, 2013 8:06 AM
    Monday, December 31, 2012 7:13 AM
  • The exception message you are seeing occurs if the first parameter 'txtBoxTwo' that you are passing to Find is either NULL or Empty. So, check whether txtBoxTwo is NULL or Empty and assign valid value. Anyways, I assume that the value of txtBoxTwo is "txtBoxTwo" and updated your code like below,

    string txtBoxTwo = "txtBoxTwo";
    var controls = this.Controls.Find(txtBoxTwo, true); TextBox branchName = controls.Length > 0 ? controls.First() as TextBox : null;

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by Lisa Zhu Friday, January 4, 2013 8:06 AM
    Monday, December 31, 2012 7:32 AM
  • try this 

    TextBox branchName = (this.Controls.Find(txtBoxTwo, true).First() as TextBox)== null : new TextBox() ? (this.Controls.Find(txtBoxTwo, true).First() as TextBox);

    good luck






    Please mark the post as an answer that helps/solves your problem.

    Monday, December 31, 2012 8:42 AM