Gaurav Pal

Gaurav Pal

  • NA
  • 306
  • 66k

WCF Web service result format

Aug 6 2015 2:54 AM
Hi, I am using following web service and getting following result in json format but i want combine result for different -2 services 
 
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;
namespace TownGreen
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "LoginWebService" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select LoginWebService.svc or LoginWebService.svc.cs at the Solution Explorer and start debugging.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class LoginWebService : ILoginWebService
{
public bool Login(string uname, string pwd)
{
bool bresult;
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.CommandText = "select * from UserList where UserName=@username and Password=@pwd";
cmd.Connection = con;
cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value = uname;
cmd.Parameters.Add("@pwd", SqlDbType.VarChar, 50).Value = pwd;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read() == true)
{
bresult = true;
}
else
{
bresult = false;
}
con.Close();
return bresult;
}
public String[] GetCategory()
{
var names = new List<string>();
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
//SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123");
try
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = "Select CategoryName from CategoryTG order by CategoryName ";
//myCommand.Parameters.AddWithValue("@name", nameFilter);
SqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
names.Add(myReader["CategoryName"].ToString());
}
}
catch (Exception ex)
{
//Console.WriteLine(ex.Message);
}
finally
{
myConnection.Close();
}
return names.ToArray();
}
public String[] GetSubCategory()
{
var names = new List<string>();
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
//SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123");
try
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = "Select CategoryName,SubCategoryName from SubCategoryTG order by CategoryName ";
//myCommand.Parameters.AddWithValue("@name", nameFilter);
SqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
names.Add("Category: " + myReader["CategoryName"].ToString());
names.Add("SubCategory: " + myReader["SubCategoryName"].ToString());
}
}
catch (Exception ex)
{
//Console.WriteLine(ex.Message);
}
finally
{
myConnection.Close();
}
return names.ToArray();
}
}
}
I am getting followung result from my service method
 
Login   =   true
 
GetCategory =  
 
[
"Eat",
"Entertainment",
"Hobbies",
"Sports"
]
GetSubCategory =  
[
"Category: Eat",
"SubCategory: Pubs",
"Category: Eat",
"SubCategory: Eat out ",
"Category: Entertainment",
"SubCategory: Dance",
"Category: Hobbies",
"SubCategory: Test",
"Category: Sports",
"SubCategory: Tennis",
"Category: Sports",
"SubCategory: Golf",
"Category: Sports",
"SubCategory: Soccer"
]
 
But  i want following result
 
 {
"Login" "True"
"Categories" |
{
"Category" "Eat",
"Entertainment",
"Hobbies",
"Sports"
}
{"Category" "Eat"
"SubCategory"|
"Eat Out"
"Pubs"
}
{"Category" "Sports"
"SubCategory"|
"Tennis"
"Golf"
}
}
}

Answers (6)