Saturday, July 9, 2016

IsDBNull() Equivalent in C#

When working with DBNull values in VB.NET you can check if that object is of type DBNull using IsDBNull(object). So, to do this in C#, I have two options to do this. The first one is to get the type of that object and compare it with typeof(System.DBNull).
Code
1
2
3
4
5
var attribute = element.getAttribute("data-mixvids");
if(attribute.GetType() != typeof(System.DBNull)) 
{
      lstElementVids.Add(element.getAttribute("data-mixvids"));
}

The other one is presented in MSDN docs using DBNull.Value.Equals(object) which returns true.
Code
1
2
3
4
5
var attribute = element.getAttribute("data-mixvids");
if(!System.DBNull.Value.Equals(attribute))
{
     lstElementVids.Add(element.getAttribute("data-mixvids"));
}

Reference: MSDN Convert.IsDBNull Method (Object)

0 comments:

Post a Comment