How to return multiple parameters using web method (web service) in C#?


How can I return multiple parameters of string type using web method (web service) in asp.net C#, here is my current code

[System.Web.Services.WebMethod]
public static string GetDetail(string oid, Label label)
{

string s = "select amount,amount_type from money where id='" +oid + "'";

connect con = new connect();
DataSet ds = con.get(s);
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
label.Text = ds.Tables[0].Rows[0]["a_type"].ToString();

string a=Math.Round(Convert.ToDecimal(ds.Tables[0].Rows[0][0]), 2).ToString();

return a + " " + label;
}
else
{
return "no";
}
}
else
{
return "no";
}

}

 


Asked by:- RameshwarLate
0
: 538 At:- 11/15/2017 9:43:34 AM
c# web-method web-services asmx






1 Answers
profileImage Answered by:- manish

The recommended method is to create custom data type class and return data as the class type instead of string.

Like this suppose you have this class

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

then your web method in web service should be like

[WebMethod(EnableSession = true)]
public MyDataType TestMethod(string testId)
{
  //Some Code

    
    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 };
}

Another method, not a good way but you could return an array of strings:

public string[] WebServiceMethod(...)
{
    return new string[] { data, data1 };
}

You'd then need to perform each bit of JSON deserialization separately on the client, which isn't a pleasant task

Or third solution is to check this link

2
At:- 11/15/2017 11:38:15 AM Updated at:- 11/15/2017 11:43:31 AM
good and easy answer for sending multiple data from web method 0
By : vikas_jk - at :- 3/25/2018 10:18:18 AM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use