tags:

views:

368

answers:

3

I have Written a webservice with VS2008 after I had Added Reference to that service in VS2003,I encountered a problem calling methods which return nullable values such as int? if I fill that feild with a value,the problem solves. Is There any other way to solve this problem?

Some more Information

Please Look at these extract from my own code:

public RevolverFund[] RetrieveRevolverFundList(int accountSetupOrganId, [System.Xml.Serialization.XmlIgnoreAttribute()] bool accountSetupOrganIdSpecified) 
{
    object[] results =  this.Invoke("RetrieveRevolverFundList", new object[] 
                                                                {
                                                                      accountSetupOrganId,
                                                                      accountSetupOrganIdSpecified
                                                                });
    return ((RevolverFund[])(results[0]));
}

It is generated by VS2003 automatically when I add the web reference to my solution. these lines of code located in 'Reference.cs' file. RevolverFundView is a class that has some nullable properties the exception "There is an error in XML document (1, 481)." is thrown whenever the 'Invoke' Method is called.

by the way here is declaration of RevolverFundClass

public class RevolverFund {

    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    public string Comment;

    public int EmployeeCode;

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool EmployeeCodeSpecified;

    public int Id;

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool IdSpecified;

    public int OrganId;

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool OrganIdSpecified;
}

in 'RevolverFund' class , 'EmployeeCode' and 'OrganId' properties are declared as 'Nullable Int's

**and here is The RevolverFund class difinition In VS2008**


    [DataContract]
    public class RevolverFund
    {
        private Int32 m_Id;
        [DataMember]
        public Int32 Id
        {
            get { return m_Id; }
            set { m_Id = value; }
        }

        private Int32? m_EmployeeCode;
        [DataMember]
        public Int32? EmployeeCode
        {
            get { return m_EmployeeCode; }
            set { m_EmployeeCode = value; }
        }

        private Int32? m_OrganId;
        [DataMember]
        public Int32? OrganId
        {
            get { return m_OrganId; }
            set { m_OrganId = value; }
        }

        private String m_Comment;
        [DataMember]
        public String Comment
        {
            get { return m_Comment; }
            set { m_Comment = value; }
        }
  }
+1  A: 

From the Documentation on MSDN, when the compiler boxes/unboxes Nullable, the underlying type is boxed/unboxed, rather than the object. This means that if you haven't set any value yet (i.e. HasValue property is false) then you'd potentially get a null value returned. That's a catastrophic error - 'null' is not an 'int' - which will throw an InvalidOperationException (see docs)

JBRWilkinson
Dear JBRWilkinsonI do Understand your answer completely.But I can't solve that problem yet.Please Look at some other information I Have recently added to my question to make it clearer.thanks for your help.
Mehdi.KH
A: 

Did you change your webservice from int to int? recently? If so, update your web references in your VS2003 service, then try again. You should check that the return type your VS2003 instance is expecting is int? rather than int

We have plenty of webservices that pass round nullable ints - it is possible

Chris Gill
Dear Shris Gillit is the first time that I use this webservice.I added some more information to my question.please look at them.I dont know how to check the return type in VS2003because the exception throws when the cursor reaches to 'Invoke Method' as I show it in my code sample
Mehdi.KH
A: 

In your ResolverFund class, 'EmployeeCode' and 'OrganId' are not nullable - they are just normal ints. To make them nullable that need to be defined as

public int? EmployeeCode;
public int? OrganId;

or

public Nullable<int> EmployeeCode;
public Nullable<int> OrganId;

Once you have done that you must refresh your web references from your other project, then it should work

Chris Gill
They are int? at The Source Of webservice but when I add web reference to my VS2003 Client Project , The 'reference.cs' file Generated as I said Beforeand if I add "[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]" above the EmployeeCode or OrganId another Exception will riseIt may behave like that because VS2003 knows nothing about nullable values
Mehdi.KH
Can you post the RevolverFund class definition from your VS2008 project?
Chris Gill
I edited my question and added The RevolverFund difinition to itPlease see
Mehdi.KH
Change your EmployeeCode and OrganId to nullable as I have in this answer, rather than setting the attribute to nullable and retry. Just adding the attribute doesn't make the int nullable in your 2008 code - it just tells the web service calling it that it should be nullable (which is a lie)
Chris Gill
VS2003 does not support nullable types.
John Saunders
Oh yes, VS2003 only supports .Net 1.1 (or 1.0), and nullable types were only introduced in .Net framework 2.0. Sorry! If you need to use VS2003 then you will need to pass those ints as strings and deal with nulls in your business logic on both sides of the webservice call
Chris Gill
Thanks.Your way works.
Mehdi.KH