none
What is C#'s equivalent for VB's "IsDbNull"?

    Question

  • I recently transferred from VB to C#. Being familiar with some VB functions, sometimes I don't know how to use it in C#.

    My question seems to be silly. But still hoping that you can give me a hand. Many thks!

     

    Monday, June 19, 2006 3:44 AM

Answers

  • check

    if(your value == System.DBNull.Value)

    Monday, June 19, 2006 4:42 AM
  • the IDataReader object has a IsDbNull method.. e.g

    IDataReader reader;

    reader = ExecuteReader("SP",args);

    if (!reader.IsDbNull(0))
      tmp = reader.GetString(0);
    Wednesday, June 21, 2006 1:01 AM
  • Mr.Saxena,

    My code goes now like: if (db.dr["articleid"]==System.DbNull.Value)

    But another error occured:"The type or namespace name'DbNull' does not exist in the class or namespace'System'(are you missing an assembly reference?) "

    There is directive of "using System;" at the beginning of the program.

    Monday, June 19, 2006 8:20 AM
  • OH, now i get it!

    here letter "b" is also capital! It should be "System.DBNull" instead of "System.DbNull".

    Anyway, thks for your help!

     

     

    Monday, June 19, 2006 8:22 AM
  • The most direct equivalent in C# is System.Convert.IsDBNull.

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter
    Instant C++: VB to C++ converter
    Clear VB: Cleans up VB.NET code

    Monday, June 19, 2006 1:59 PM

All replies

  • check

    if(your value == System.DBNull.Value)

    Monday, June 19, 2006 4:42 AM
  • Mr.Saxena,

    My code goes now like: if (db.dr["articleid"]==System.DbNull.Value)

    But another error occured:"The type or namespace name'DbNull' does not exist in the class or namespace'System'(are you missing an assembly reference?) "

    There is directive of "using System;" at the beginning of the program.

    Monday, June 19, 2006 8:20 AM
  • OH, now i get it!

    here letter "b" is also capital! It should be "System.DBNull" instead of "System.DbNull".

    Anyway, thks for your help!

     

     

    Monday, June 19, 2006 8:22 AM
  • The most direct equivalent in C# is System.Convert.IsDBNull.

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter
    Instant C++: VB to C++ converter
    Clear VB: Cleans up VB.NET code

    Monday, June 19, 2006 1:59 PM
  • Try using like

    if ( yourValue == null )

    {

     

    }

    Thanks

    Asad Iqbal

    Tuesday, June 20, 2006 9:34 AM
  • the IDataReader object has a IsDbNull method.. e.g

    IDataReader reader;

    reader = ExecuteReader("SP",args);

    if (!reader.IsDbNull(0))
      tmp = reader.GetString(0);
    Wednesday, June 21, 2006 1:01 AM
  •  Asad Iqbal wrote:

    Try using like

    if ( yourValue == null )

    {

     

    }

    Thanks

    Asad Iqbal



    This does not works, because the user is comparing to Database null and not null. There is a differences between these two.


    Tuesday, May 27, 2008 9:13 AM
  • Mr. dreadjr

    this code not useful for me when i used this code in application that time i got error 
    'the name args does not exist in current context' & reader not take 2 arguments.
    Friday, October 09, 2009 11:06 AM
  • Hi Guys,

    Firstly, I didn't mean to answer that as 'answered'.

    Secondly, the answer in C#

     

    I had to do this the other day and here is me code

    [code]

     if (reader["randomField"] != DBNull.Value)
       {
             int _value = Convert.ToInt32(reader["randomField"]);
        }
        else
        {

            //What ever

         }

     

    [/code]

     

    You must keep in mind that DBNull is the same as null in sql, therefore, you cannot use == or != or <>, this is not an ordinary value, its a three-valued logic (3VL) object or item.

     

    ;)

    Friday, January 07, 2011 12:24 PM
  • My suggestion:  If you're doing a language conversion (vb.net to somethingElse.net), then you will have many iterations of replacing vb.net's global [object] functions with other equivalents.  The replacement that I use for IsDBNull() is DataRow.IsNull(String columnName).  It's a natural fit, and after that conversion, the new code will convert to C# [or nearly anything else .net] easily.

    For scripts within the html, consider converting IsDBNull() to DbNull.Value.Equals(). This will convert well when you go to the other .net language.

    • Edited by aptdude Monday, April 02, 2012 6:50 PM
    Monday, April 02, 2012 2:19 PM