Click here to Skip to main content
15,891,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,
Hi I have one WCF service and one single method named public GetDataUsingMethodList() in the service.It's working fine when it's return single response.
But i want to return multiple response i.e. List and String both from Method GetDataUsingMethodList().

Please help me on this.
Posted
Updated 30-Aug-12 19:17pm
v2

1 solution

Use DataContract.
For Example:
C#
[DataContract]
public class Result 
{
    [DataMember]
    public List<int> ListOutPut { get;set;}

    [DataMember]
    public String StringOutPut { get;set;}
}

Now, use this as the return type in your contract, i.e. both in service class and its interface.
For example:
C#
public interface IService
{
    Result GetDataUsingMethodList();
}

public class Service : IService
{
    public Result GetDataUsingMethodList()
    {
        // create the result and return it.
    }
}

This way, you can achieve multiple types within the output.
 
Share this answer
 
v2
Comments
Sanjeev Alamuri 31-Aug-12 1:34am    
Thanks @pramodhegde88. and please let me know the return value! i have a list and string!
pramod.hegde 31-Aug-12 1:50am    
Hi,
Use WCFTestClient to test this service and to get the exact output. This is a tool which comes with Visual Studio.
It will be available in
..\Microsoft Visual Studio 10.0\Common7\IDE\WcfTestClient.exe (10.0 is what i am using..check your version number and search for WcfTextClient)
Sanjeev Alamuri 31-Aug-12 2:07am    
dataTable = new DataTable();
cmd.Connection = conn;
cmd.CommandText = "SELECT * FROM tbl_TrainDetails";
ad = new SqlDataAdapter(cmd);
ad.Fill(dataTable);
List<TrainDetails> TrainD = new List<TrainDetails>();
for (int i = 0; i < dataTable.Rows.Count; i++)
{
TrainDetails td = new TrainDetails();
td.TrainNumber1 = Convert.ToInt32(dataTable.Rows[i]["TrainNumber"].ToString());
td.TrainName1 = dataTable.Rows[i]["TrainName"].ToString();
td.FromStation1 = dataTable.Rows[i]["FromStation"].ToString();
td.ToStation1 = dataTable.Rows[i]["ToStation"].ToString();
td.Depature1 = dataTable.Rows[i]["Depature"].ToString();
td.Arrival1 = dataTable.Rows[i]["Arrival"].ToString();
td.Status1 = dataTable.Rows[i]["Status"].ToString();
TrainD.Add(td);
}


Here i have to return "TrainD" list and mode type as string.

so how to set return value?
pramod.hegde 31-Aug-12 2:13am    
[DataContract]
public class Result
{
[DataMember]
public List<TrainDetails> Details{ get;set;}

[DataMember]
public String ModeType { get;set;}
}

public Result GetDataUsingMethodList()
{
// have the required details here
dataTable = new DataTable();
cmd.Connection = conn;
cmd.CommandText = "SELECT * FROM tbl_TrainDetails";
ad = new SqlDataAdapter(cmd);
ad.Fill(dataTable);
List<TrainDetails> TrainD = new List<TrainDetails>();
for (int i = 0; i < dataTable.Rows.Count; i++)
{
TrainDetails td = new TrainDetails();
td.TrainNumber1 = Convert.ToInt32(dataTable.Rows[i] ["TrainNumber"].ToString());
td.TrainName1 = dataTable.Rows[i]["TrainName"].ToString();
td.FromStation1 = dataTable.Rows[i]["FromStation"].ToString();
td.ToStation1 = dataTable.Rows[i]["ToStation"].ToString();
td.Depature1 = dataTable.Rows[i]["Depature"].ToString();
td.Arrival1 = dataTable.Rows[i]["Arrival"].ToString();
td.Status1 = dataTable.Rows[i]["Status"].ToString();
TrainD.Add(td);
}

return new Result()
{
Details = new List<TrainDetails>(TrainD),
ModeType = "SomeModeType"
};
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900