vml19
vml19

Reputation: 3864

Check for DbNull in C#

I am finding difficulties in validating the following

if(personDS.person[0].IdFk!= DBNull.Value)

this is the compile time error - cannot be applied to operands of type 'System.Guid and 'System.Dbnull'

Upvotes: 2

Views: 9024

Answers (5)

Giorgio Minardi
Giorgio Minardi

Reputation: 2775

Is IdFk declared as nullable ? Es:

public Guid? Idfk 

If so you can :

if(personDS.person[0].IdFk.HasValue)

Upvotes: 0

Ɖiamond ǤeezeƦ
Ɖiamond ǤeezeƦ

Reputation: 3331

Id is a Guid, which is a Value type and will therefore never equal DBNull.Value. Before I can fully help, I will need to know the types of personDS (DataSet maybe?) and person (DataTable maybe). Assuming so, then maybe:

if (!personDS.person[0].IsNull("IdFK"))

If person is not a DataTable but a class then check for either IdFk == Guid.Empty, if IdFk is a Guid, or IdFk == null if IdFk is Guid?.

Upvotes: 0

Akshita
Akshita

Reputation: 867

try

if(!(personDS.person[0].IdFk is System.DBNull))

Upvotes: 0

Jason
Jason

Reputation: 15931

You can also check against an empty Guid

if (personDS.person[0].IdFk == Guid.Empty )

Upvotes: 0

Alex Dn
Alex Dn

Reputation: 5553

I think you should check:

if(personDS.person[0].IsIdFkNull())

Upvotes: 4

Related Questions