Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return two values from webservice

Is is possible to return two values from a WebService to jQuery.

I tried like

[WebMethod(EnableSession = true)]
public string testing(string testId)
{
    string data = string.Empty;
    string data1 = string.Empty;
    List<test1> datalist1 = new List<test1>();
    List<test> datalist = new List<test>();

    //coding
    data = jsonSerialize.Serialize(datalist1);
    data1 = jsonSerialize.Serialize(datalist);
    return [data,data1];
}

but its showing error....how can we return two values from webservice here.....

like image 749
shanish Avatar asked Apr 17 '12 13:04

shanish


People also ask

Can we return two values in return statement?

No, you can not have two returns in a function, the first return will exit the function you will need to create an object.

How can I return multiple values?

You can return multiple values by bundling those values into a dictionary, tuple, or a list. These data types let you store multiple similar values. You can extract individual values from them in your main program. Or, you can pass multiple values and separate them with commas.

Can we use 2 return in a function?

Create a function getPerson(). As you already know a function can return a single variable, but it can also return multiple variables.

Can you return multiple strings?

No you cannot return more than one value. But you can send an array of strings containing all those strings. Save this answer.


1 Answers

Another way is to create a custom data type that has the two return values you want:

[Serializable]
public sealed class MyData
{
    public string Data { get; set; }
    public string Data1 { get; set; }
}

...

[WebMethod(EnableSession = true)]
public MyData testing(string testId)
{
    string data = string.Empty;
    string data1 = string.Empty;
    List<test1> datalist1 = new List<test1>();
    List<test> datalist = new List<test>();

    //coding
    data = jsonSerialize.Serialize(datalist1);
    data1 = jsonSerialize.Serialize(datalist);
    return new MyData { Data = data, Data1 = data1 };
}

OR

[Serializable]
public sealed class MyData
{
    public List<test> Data { get; set; }
    public List<test1> Data1 { get; set; }
}

...

[WebMethod(EnableSession = true)]
public string testing(string testId)
{
    MyData data = new MyData();
    string alldata = string.Empty;
    List<test1> datalist1 = new List<test1>();
    List<test> datalist = new List<test>();

    //coding
    data.Data = datalist1;
    data.Data1 = datalist;
    alldata = jsonSerialize.Serialize(data);
    return alldata;
}
like image 178
Jesse C. Slicer Avatar answered Sep 30 '22 16:09

Jesse C. Slicer