Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1205
Ultra Combo - checking for null
posted

When checking the below column for a value and the value is null, this error is returned:

Object reference not set to an instance of an object.

Values other than null process correctly. I also checked for a DBNull.Value.

I know of work arounds but it's not my preference.

Any help/ideas are appreciated.

Thanks.

if (ucUserName.ActiveRow.Cells["UserID"].Value == null) //DBNull.Value

 

{

 

 

 

GridLayout.UserID = 0;

}

 

 

else

{

 

 

 

 

GridLayout.UserID = Convert.ToInt32(ucUserName.ActiveRow.Cells["UserID"].Value.ToString

());

}

 

 

 

 

 

Parents
No Data
Reply
  • 20872
    Suggested Answer
    Offline posted

    Hello 2001plus,

    I think that at that particular moment you don't have an active row and that's why you are getting a null exception. In order to make sure that your code will be executed correctly, you could add a null check in your if clause before your code like:

     if (ucUserName.ActiveRow !=null && ucUserName.ActiveRow.Cells["UserID"].Value == null)

    Please feel free to let me know if you have any other questions with this matter.

Children