views:

123

answers:

1

I use VB to get data through my form. I have some optional fields in my form and I have a problem with the following code:

MsgBox(myXPathNavigator.SelectSingleNode( _
  "/my:Status/my:Questions/my:Questions1", Me.NamespaceManager _
).IsNode.ToString)

When the optional field 'Questions1' is inserted into the form I get the value 'true' by the IsNode() function.

If the field it is not inserted I have an exception stating that the reference is not correct (and it is indeed true). Is there a way to verify about a node, whether it is present or not in my form?

Thanks in advance, Sun

+2  A: 

Just don't do it in one step. SelectSingleNode() returns Nothing if the XPath was not found. You must catch that condition separately.

Dim q As XPathNavigator
Dim path as String

path = "/my:Status/my:Questions/my:Questions1"
q = myXPathNavigator.SelectSingleNode(path, Me.NamespaceManager)

If Not q Is Nothing Then
  MsgBox(q.ToString)
End If
Tomalak
The problem persists. The issue is in SelectSingleNode and note in MdgBox.
Sunscreen
@Sunscreen: What is the exact exception you get?
Tomalak
A pop-up msg appears when the tag is no tpresent in my form. Explicitely says:System.NullReferenceExceptionObject reference is not set to an instance of an objectHow can I catch it instead of appearing as a pop-up?Thanks
Sunscreen
@Sunscreen: Go through your program step by step with the debugger. On which line does the problem occur, exactly?
Tomalak
Ok, I was getting the value all the time thats wht the esception was appearing. Now, with your code though it never gets tha node, whether it is inserted or not q is 'nothing'. (Set is not valid statement so i just tried 'q = myXPath...)
Sunscreen
@Sunscreen: You were talking about VBA in your question title and tags, but obviously you are using VB.NET. These two are *very* different animals. So… yes, there is no `Set` statement in VB.NET. I'll remove the VBA references.
Tomalak
Oh sorry. IT WORKS. I had wrong node. THANKS A BUNCH.
Sunscreen
@Sunscreen: I thought so. ;-) Glad you got it working.
Tomalak