Chandrasekhar Sahoo
Chandrasekhar Sahoo

Reputation: 191

What is the C# equivalent of VB.Net " IsDBNull"

In VB.Net you can write :

If Not IsDBNull(oCustomerNameDataRow(0)) Then
    cbCustomerName.Items.Add(oCustomerNameDataRow(0).ToString
End If

What is the equivalent of method IsDBNull in C#?

Upvotes: 16

Views: 16532

Answers (3)

AminRostami
AminRostami

Reputation: 2772

try this:

create the Extension Method. follow this:

public static bool IsDBNull(this object val)
{
     return Convert.IsDBNull(val);
}

and the use from this Extension Method.

if(oCustomerNameDataRow[0].IsDBNull())
{
      // ...
}

I hope useful.

Upvotes: 0

I would say the the equivalent of the IsDBNull method (Microsoft.VisualBasic.Information) located in the Microsoft.VisualBasic assembley

Public Function IsDBNull(ByVal Expression As Object) As Boolean
    If Expression Is Nothing Then
        Return False
    ElseIf TypeOf Expression Is System.DBNull Then
        Return True
    Else
        Return False
    End If
End Function
Dim result As Boolean = IsDBNull(Nothing)

is the IsDBNull method (System.Convert) located in the mscorlib assembley:

public static bool IsDBNull(object value) {
    if (value == System.DBNull.Value) return true;
    IConvertible convertible = value as IConvertible;
    return convertible != null? convertible.GetTypeCode() == TypeCode.DBNull: false;
}
bool result = System.Convert.IsDBNull(null);

Upvotes: 9

if (!DBNull.Value.Equals(oCustomerNameDataRow[0]))
{
  //something
}

MSDN (DBNull.Value)

Upvotes: 17

Related Questions