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
: 6184 At:- 11/15/2017 9:43:34 AM
c# web-method web-services asmx







2 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

3
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
Returning Multiple values using Class is better solution I think. 0
By : neena - at :- 6/28/2021 11:18:45 AM


profileImage Answered by:- vikas_jk

You can simply return multiple values in web-api in C# as below

 [HttpGet]
 [Route("api/getexample")]
 public IHttpActionResult getAll()
 {
    
   //some code here
    
    return Ok(new { data1List, data2List, someValue });
 }

That should work.

0
At:- 10/17/2022 7:46:21 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