2

I am calling a WCF method from an ASP.Net page, but I am getting format exception when WCF tries to deserialize the recordIds parameter received from JavaScript.

The first parameter passed to the WCF method needs to be of List type. Is there something wrong I have done in using JSON.stringify?

Javascript Code to call WCF

       function Update() {
        var myarray1 = new Array();
        myarray1[0] = 1;
        myarray1[1] = 11;
        myarray1[2] = 14;
        WCFService1.AJAXEnabledService.BatchUpdateRecords(
          JSON.stringify({recordIDs: myarray1}) , "ddsd", "gggg", 
          updateGrid, OnError);
    }

WCF method being called by above JavaScript

   [OperationContract]
    public bool BatchUpdateRecords(List<int> recordIds, string columnNameToUpdate, string columnValue)
    {

        DataTable tierIDsTable = new DataTable("RecordIds");
        tierIDsTable.Columns.Add(new DataColumn("Integer", typeof(Int32)));
        tierIDsTable.PrimaryKey = new DataColumn[] { tierIDsTable.Columns["TierId"] };

        foreach (int recordId in recordIds)
        {
            tierIDsTable.Rows.Add(recordId);
        }
        return true;
    }

2 Answers 2

1

Not 100% sure, but have you tried this?

WCFService1.AJAXEnabledService.BatchUpdateRecords(
    myarray1,
    "ddsd",
    "gggg",
    updateGrid, OnError);
Sign up to request clarification or add additional context in comments.

2 Comments

WOW. You are 100% correct. Thanks for your quick and to the point answer. Just pass an array if the WCF needs a List<T> parameter. It can't be simpler than that.
@Sunil It's been a long time since I used ajax-enabled web services, so I wasn't sure if I was forgetting some detail. Glad to help.
1

The issue (without knowing the error that you are receiving) is most likely that you are trying to pass in multiple parameters types. WCF does not usually support and expects an object instead. Create a response class with your parameters and use that instead.

public class ResponseObject 
{
    public List<int> recordIds { get; set; } 
    public string columnNameToUpdate { get; set; }
    public string columnValue { get; set; }
}

Use this object as your parameter

public bool BatchUpdateRecords(ResponseObject responseObject) 
{...

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.