Question Soap Request - sending a "null" as an integer

Joined
Jun 2, 2011
Messages
16
Programming Experience
3-5
I am working with a Webservice api. The primary key field demands an Integer, but to create a new record, I have to send it a null value.

My understanding is that .Net defines strongly typed, native Data Types, and the Integer is 32 Bits, so 0 to 4 Million-what-ever-it-is (unsigned). I cannot figure a way to express Null in those 32 to bits (to describe my problem, at the lowest level I can).

In other words, what is a Null value in a VB.Net Integer?

I have tried to play around with Nullable(of Integer) (or Integer$), but I cannot express that as a Null. If I try to leave it undefined, I get a Compile Time error of "Nullable type must have a value" (An ironic and infuriating error if there ever was one).

I am quite sure this is a common problem. How do I express Null in an integer to a database through a Soap Request? The node "PrimaryKey" contains the value "null" as its inner XML in a proper Soap Request (as I have found by debugging with Fiddler2), but how do I express this without grabbing the Soap Request and doing some text editing?
 
I have tried to play around with Nullable(of Integer) (or Integer$), but I cannot express that as a Null. If I try to leave it undefined, I get a Compile Time error of "Nullable type must have a value" (An ironic and infuriating error if there ever was one).
That means that you are using the Nullable(Of Integer) type incorrectly. Every Nullable(Of T) object has a Value property of type T. That means that a Nullable(Of Integer) has a Value property of type Integer. Now, we know that an Integer must always have a value so, if your Nullable(Of Integer) object is null then how can its Value property return an Integer? That's why you are getting that exception: you MUST NOT get the Value property if the object is null.

To that end, a Nullable(Of T) object also has a HasValue property, which is type Boolean. If HasValue is True then Value will return the actual value, while if HasValue is False then Value will throw an exception like the one you saw. You should use code along these lines:
If myNullable.HasValue Then
    MessageBox.Show("The value is " & myNullable.Value.ToString())
Else
    MessageBox.Show("There is no value")
End If
If you were using nullable value types like that with ADO.NET then you could do something like this:
myCommand.Parameters.Add("@Number", SqlDbType.Int, 4).Value = If(myNullable.HasValue, myNullable.Value, CObj(DBNull.Value))
 
You sort of re-expressed my problem. I have to express a null value. I am not trying to read a value, I am trying to write it.

I am not using ADO, nor am I familiar with it. Maybe that is my problem here??
 
Have you added a Web Reference or a Service Reference? The proxy automatically uses Nullables if that is what the service defines, so for a nullable parameter you would pass Nothing if no value were to be set.
The node "PrimaryKey" contains the value "null" as its inner XML in a proper Soap Request (as I have found by debugging with Fiddler2)
Not what I'm seeing when using .Net web services, but your service may use a proprietary format, for example the parameter/property may be String type where you have to set the string "null" to indicate no value according to that service protocol.
 
Thank you for trying to help me with this.

I have the webservice all set up, and I can use it for all kinds of things. Except to save a record, and the problem is, the PrimaryKey field needs a null value to recognize that it is a new record. However, the Webservice defines the PrimaryKey field as an Int32 (I verified this with PrimaryKeyField.GetType().ToString()).

All I know beyond that is that when I modify the Soap Request, to read <PrimaryKey>null</PrimaryKey>, the Record saves.

I can find no way to reach this result through the Webservice.

Is this possibly because the writers of the Webservice focused on a more loosly typed language and did not take into account the fact that VB will not let me express null (or any other state besides 0 to 4 million something -unsigned) in 32 bits?

Am I to understand from your recent response that if the writers of the Webservice did not define for .Net's Nullable types, then I cannot use them? I assume this is true, but there may be something universal about null that I do not understand.
 
Method parameters and return values is defined by the service schema, when you create the .Net proxy it automatically detects this and use Nullable types where appropriate. In Soap protocol a null field is universally expressed as an empty node with xsi:nil="true" attribute set, this is also automatically taken care of by the .Net proxy. So a node with value "null" does not fit into this picture in relation to nullable types, as I said it sounds more as a string field where "null" is an arbitrary value with special meaning to that service.
 
Thank you so much for trying to help me. I found the solution, but I wanted to ask you what you thought about it.

For the PrimaryKey Field (as well as many others) there is an (undocumented) PrimaryKeySpecified Field. There is also a ClientNameSpecified field, and ZipCodeSpecified field, and so forth.

Does it seem more likely that this is natural to .Net as a way to express Null in these fields, or does it seem more likely that these fields are strictly native to the proprietary API that I am using, perhaps as a work around for populating a Null value from strongly typed languages such as VB.Net.

Again thank you, and a bit more of your expertise on this would be greatly appreciated.
 
That sounds awkward and unnatural to me, but by the sound of it seems to be boolean fields to determine if other fields is to be used or not.
 
Back
Top