You must Sign In to post a response.
  • Category: WCF

    Return value Format of my web service

    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"
    }
    }
    }
  • #761307
    1. Create new object like "LoinCat" and wrap two exsiting object
    2. You can use Linq<> for combine Cat and SubCat

    By Nathan
    Direction is important than speed

  • #761308
    i am not using LINQ please provide some example related to my problem

  • #761311
    1. you can try to deserialize the JSON
    2 Enumerate the list then create the object as you required

    By Nathan
    Direction is important than speed

  • #761314
    i am not using LINQ please provide some example related to my problem

  • #761315
    class Category
    {
    String[] SubCategories;
    }
    class ResultSet
    {
    bool Login;
    Category[] Categories;
    }
    like this


Sign In to post your comments