3

Need to send the values of a Sqlite table to the server via an AJAX call to a web service. I can't figure out how to package the rows of this table in a way that the web service will accept it.

Web Method Signature

[WebMethod]
    public void CVW(string[][] qAndA)
    {}

Client Side

var qAndA = new Array();
tx.executeSql("Select * From CVW Where CallNumber=?", [call], function (tx, result) {
                        var i = 0;
                        $.each(result.rows, function () {
                            qAndA[i] = result.rows.item(i);
                            i++;
                        });
$.ajax({
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json",
                            url: "Service.asmx/CVW",
                            data: JSON.stringify({ qAndA: qAndA }),
                            success: function (data) {
                            },
                            error: function (xhr, status, error) {
                                var err = eval("(" + xhr.responseText + ")");
                                alert(err.Message);
                            }
                        });
                    }, onSQLError);

This gives me No parameterless constructor defined for type System.String. I've tried changing the web method parameter to just string[] and List and same thing.

When I look at what is being sent in Fire Bug I see that it is sending over as Object[object Object][object Object][object Object]... for as many [object Object] as there are rows returned in the Sqlite Query. What is the corresponding type in C# that I can use as my parameter type?

EDIT: Final Solution

In the javascript I had to change

data: JSON.stringify({ qAndA: qAndA }),

to

var json = JSON.stringify(qAndA);
data: JSON.stringify({ qAndA: json });

and changed the Web Method thusly

 [WebMethod]
    public void CVW(string qAndA)

JavaScriptSerializer ser = new JavaScriptSerializer();
List<CVWQuestion> list = ser.Deserialize<List<CVWQuestion>>(qAndA);

[Serializable]
public class CVWQuestion
{
    public string CallNumber {get;set;}
    public string Question { get; set; }
    public string P1 { get; set; }
    public string P2 { get; set; }
    public string P3 { get; set; }
    public string P4 { get; set; }
    public string P5 { get; set; }
    public string P6 { get; set; }
}
jmease
  • 2,507
  • 5
  • 49
  • 89
  • 1
    Maybe you should create some structured objects, instead of working with arrays of arrays ? – Steve B Nov 20 '12 at 15:04
  • @SteveB Can you expand on that please? – jmease Nov 20 '12 at 15:19
  • 1
    Create some simple and serializable classes, that holds your data. Use this class as argument of your webservice, and, on the client side, create json object that matches your structure – Steve B Nov 20 '12 at 15:29
  • I think I'm with you up in the JSON object matching my structure. I'm self taught and the entirety of my knowledge of JSON is being used in my code currently. I've created a new class in my web service that is decorated with the serializable attribute and has been changed to be my parameter type. How do I create a corresponding JSON object? – jmease Nov 20 '12 at 15:59
  • 1
    to find the format, you may add a very simple method in you web service, like `public YourClass GetSample() { return new YourClass { Prop1 = "value1", Prop2 = 42 }; }` (adjust to your structure). Then query this method on the client side, and you will see the JSon structure – Steve B Nov 20 '12 at 16:01
  • @SteveB If you submit as answer I will accept – jmease Nov 20 '12 at 16:54

3 Answers3

0

Please see my edited post for the code that finally worked for me. Found in large part to @SteveB, but never submitted as answer so I couldn't accept it.

jmease
  • 2,507
  • 5
  • 49
  • 89
-1

Change the String[][] parameter to just a String. The data being sent it just a string. You need to convert it from a stringified JSON object to something you can parse. Look into JObject.

Converting from your stringified JSON looks something like...

JObject.parse(myJSONString)

I would change the webservice to something like

public void CVW(string qAndAStringJson){

   Object qAndAObj = JObject.parse(qAndAStringJson);
   string someVar = qAndAObj["someStringIndex"];

}

Check out the documentation on Stringify here. Your call to Stringify is changing the data into a string representing a JSON object.

I took this from the link.

JSON.stringify({x: 5, y: 6}); // '{"x":5,"y":6}' or '{"y":6,"x":5}'

Notice it's just one long string.

So if you call JObject.parse() on '{"x":5,"y":6}' in your webservice method you can make calls like

string obj = JOBject.Parse('{"x":5,"y":6}');

string result = obj["x"]; //result = 5

spots
  • 2,483
  • 5
  • 23
  • 38
  • "Type System.String is not supported for deserialization of an array." Based on what I can see from firebug, what is being passed is an Object[object Object][object Object]....for as many objects as there are rows returned in the query. – jmease Nov 20 '12 at 14:40
  • JSON.stringify({ qAndA: qAndA }) is sending a string like "{"stuff":{"name":"1", "id":"stuff1"}}" Stringify converts the data to one long string, which is why you should need to have the webservice have a string parameter. Try saving the result of JSON.stringify({ qAndA: qAndA }) into a variable and playing with it. – spots Nov 20 '12 at 14:47
  • Play with it how? I don't understand what you are telling me to make it do? What should it look like? – jmease Nov 20 '12 at 14:51
-1

You can pass your 2d array in following format

{
    "values[0][0]": "Some value",
    "values[1][0]": "Some value",
    "values[0][1]": "Some value",
    "values[1][2]": "Some value",
    "values[2][0]": "Some value",
    "values[0][2]": "Some value",
    "values[2][3]": "Some value",
    ...
}

or, alternatively, as spots mentioned, use JSON.stringify.

Also, take a look at this post, Parse JSON in C#, which might be helpful to you.

Community
  • 1
  • 1
woodykiddy
  • 6,074
  • 16
  • 59
  • 100