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

11,138

Solution 1

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

MSDN (DBNull.Value)

Solution 2

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);
Share:
11,138

Related videos on Youtube

Chandrasekhar Sahoo
Author by

Chandrasekhar Sahoo

Updated on June 04, 2022

Comments

  • Chandrasekhar Sahoo
    Chandrasekhar Sahoo almost 2 years

    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#?

    • majjam
      majjam about 9 years
    • Random Dev
      Random Dev about 9 years
      to the downvoters: this is a viable and good question - either find a reason to flag it (for example it might have been asked before) or stop downvoting things - thank you
    • Rick Davin
      Rick Davin about 9 years
      I did not downvote but it this seems to be something that could have easily been looked up on MSDN.
  • Random Dev
    Random Dev about 9 years
    yep - from System.DBNull
  • Kiquenet
    Kiquenet about 4 years
    NOT SAME FOR VB.NET NET 4.5 IsDBNull: If (Expression IsNot Nothing) Then flag = If(Not TypeOf Expression Is DBNull, False, True) Else flag = False End If