Home All Groups Group Topic Archive Search About

IsDBNull check -- translation from VB.Net to C#

Author
11 Jul 2006 6:50 AM
Earl
In VB.Net, I often check for a null in a dataview column before assigning a
value from that column to a variable:

   If Not IsDBNull(dvContacts.Item(0).Item("LastName")) Then
    strLastName = CStr(dvContacts.Item(0).Item("LastName"))
   Else
    strLastName = ""
   End If

But I have not figured out how to make the same IsDbNull check in C#.

Author
11 Jul 2006 8:38 AM
sharmilar
Hi,

You can use the code below

if (! (System.Convert.IsDBNull(dvContacts.Item ["LastName"])))
{
    strLastName = System.Convert.ToString(dvContacts.Item ["LastName"]);
}
  else
{
    strLastName = "" ;
}

Thanks
Sharmila
Author
11 Jul 2006 1:37 PM
Peter Johnson
Alternatively to the above comment from Sharmila.

You could create a strongly typed dataset, then any fields which are
nullable will have methods automatically created for them by the
xsd.exe.

So if you had a column called LastName, the following would be how you
would check for nulls.

if (datasetName.table1[0].IsLastNameNull())
{
        LastName = string.Empty;
}
else
{
        LastName = datasetName.table1[0].LastName;
}

The good thing about creating strongly typed datasets is that it does a
lot of the code for you.  And also it's not using the legacy Convert
methods which were only included to allow VB6 developers to quickly
move accross to the .net platform.

Also Typed datasets provide intellisense support so you never mistype
your fieldnames, and they can also be used by either VB.net or C#.

It's taken me a long time to get used to them but well worth my time
spent investigating them.

Here is a good article on strongly typed datasets near the bottom there
is a section which also explains about the IsNull functionality.

http://msdn.microsoft.com/msdnmag/issues/04/12/DataPoints/

Regards,

Pete

sharmi***@syncfusion.com wrote:
Show quote
> Hi,
>
> You can use the code below
>
>  if (! (System.Convert.IsDBNull(dvContacts.Item ["LastName"])))
>  {
>     strLastName = System.Convert.ToString(dvContacts.Item ["LastName"]);
>  }
>   else
>  {
>     strLastName = "" ;
>  }
>
> Thanks
> Sharmila
Author
11 Jul 2006 5:52 PM
Earl
Thanks to both you and Peter. This is the syntax I will use. I've never been
happy using the strongly typed dataset functionality, altho I'm sure it has
its place.


<sharmi***@syncfusion.com> wrote in message
Show quote
news:1152607079.985678.53350@m79g2000cwm.googlegroups.com...
> Hi,
>
> You can use the code below
>
> if (! (System.Convert.IsDBNull(dvContacts.Item ["LastName"])))
> {
> strLastName = System.Convert.ToString(dvContacts.Item ["LastName"]);
> }
>  else
> {
> strLastName = "" ;
> }
>
> Thanks
> Sharmila
>
Author
13 Jul 2006 3:07 PM
Francois Bonin [C# MVP]
A really simple way would be:

if (!dvContacts[0].Row.IsNull("LastName"))
{
    // ......
}
else
{
    //.....
}


Show quote
"Earl" <brikshoe@newsgroups.nospam> wrote in message
news:OKUYZZLpGHA.3288@TK2MSFTNGP03.phx.gbl...
> In VB.Net, I often check for a null in a dataview column before assigning
> a value from that column to a variable:
>
>   If Not IsDBNull(dvContacts.Item(0).Item("LastName")) Then
>    strLastName = CStr(dvContacts.Item(0).Item("LastName"))
>   Else
>    strLastName = ""
>   End If
>
> But I have not figured out how to make the same IsDbNull check in C#.
>

AddThis Social Bookmark Button