0

How to pass "NULL" value into nullable field during call from JavaScript to ASP.NET WebService?

I call method in the following way from JavaScript:

ws_data.SaveTasks(eval({"MyValue":"null"}), OnSaveComplete, OnError, OnTimeOut);

When I do, I receive an error (framework calls OnError):

null is not a valid value for Int32

If instead of "null" I pass valid integer - everything works fine.

Please advise: What I do wrong? How to pass 'null' value which will be 'translated' into a NULL .NET object?

Any ideas are welcome!

P.S. Here are some details.

Declaration of essential part of MyData:

public class MyData
{
    ...
    public int? MyValue { get; set; }
}

Declaration of ASP.NET web service:

[WebService(Namespace = "...")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class ws_data : WebService
{
    [WebMethod(true)]
    public object SaveData(MyData data)
    {...}
}

And ws_data is declared via ScriptManager:

    <asp:ScriptManager runat="server">
        <Services>
            <asp:ServiceReference Path="/ws_data.asmx" />
        </Services>
    </asp:ScriptManager>
4
  • Did you tried to not passing anything? Commented Apr 1, 2014 at 2:59
  • 1
    Pass null, not string "null". Commented Apr 1, 2014 at 3:01
  • if its Nullable why do you even want to pass anything? Commented Apr 1, 2014 at 3:19
  • I didn't try to not pass anything... looks like that is an idea. But i prefer explicit declaration. Thank you for suggestions. Commented Apr 2, 2014 at 4:44

1 Answer 1

1

This is because you're passing the string null instead of the non-value null. Remove the quotes to make it an actual null.

{"MyValue": null}

I'm not sure about your eval either, I think it can be omitted as eval is supposed to take a string an interpret it as JavaScript.

ws_data.SaveTasks({"MyValue": null}, OnSaveComplete, OnError, OnTimeOut);
Sign up to request clarification or add additional context in comments.

Comments

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.