2

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'

2
  • Exception is thrown when you try to access IdFk because it's not nullable. Commented Jan 19, 2012 at 9:26
  • You might find the answer here: stackoverflow.com/questions/211436/nullable-guid Nullable GUID question. Commented Jan 19, 2012 at 9:29

5 Answers 5

4

I think you should check:

if(personDS.person[0].IsIdFkNull())
Sign up to request clarification or add additional context in comments.

Comments

0

You can also check against an empty Guid

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

Comments

0

try

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

1 Comment

If IdFk is null, it will throw exception. Check DataSet autogenerated class for implementation. The property throws and exception. Maybe it's can be changed by column property, but it's not worked for me.
0

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

Comments

0

Is IdFk declared as nullable ? Es:

public Guid? Idfk 

If so you can :

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

2 Comments

As far as I can tell this is complete nonsense, for the followingg reasons: 1) public starts with a lower p 2) the ? suffix has to be appended to the type, not the identifier 3) if personDS.person[0].IdFK indeed is null the code will throw an InvalidOperationException, else the condition will always be false, since DBNull.Value doesn't equal anything but itself and a Guid can never be of type DBNull (and it won't compile, since you can't compare Guid and DBNull without casting either one to object, which will do a reference comparison)
True, I without VS now and coding in vb this period. Made a some confusion :) Edited the response anyway, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.