Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I need to negelct the default value(string) in the output which is displayed when executing the webservice JSON.

Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;


namespace Webservice
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        public Service1()
        {
            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        //public string GetEmployees(string SearchTerm) 
        public string GetEmployees()
        {
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NSConstr"].ToString());
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT *  FROM Contact e ";
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.SelectCommand.Connection = con;
            da.Fill(dt);
            con.Close();
            
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row = null;
            foreach (DataRow rs in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, rs[col]);
                }
                rows.Add(row);
            }

            return "{ \"Cargo\": " + serializer.Serialize(rows) + "}";
        }     

        public string errmsg(Exception ex)
        {
            return "[['ERROR','" + ex.Message + "']]";
        }
    }
}


Output is;

<string>
{ "Cargo": [{"Id":1,"FirstName":"devi","LastName":"priya ","Contactno":"965577796 "},{"Id":2,"FirstName":"arun","LastName":"kumar","Contactno":"9944142109"},{"Id":3,"FirstName":"karu","LastName":"ronald","Contactno":"8883205008"}]}
</string>


the result should be as follows:

{ "Cargo": [{"Id":1,"FirstName":"devi","LastName":"priya ","Contactno":"965577796 "},{"Id":2,"FirstName":"arun","LastName":"kumar","Contactno":"9944142109"},{"Id":3,"FirstName":"karu","LastName":"ronald","Contactno":"8883205008"}]}



In the above output i dont want to see the string /string
is there is a way to omit that if so please suggest me.

Thanks in advance
Posted
Updated 3-Apr-14 3:21am
v6

1 solution

hi
the solution for the above issue is
1. instead of
public string GetEmployees()
i change it to
public Void GetEmployees()


2.instead of returning the result i used the following code

C#
this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(serializer.Serialize(new { Cargo = rows }));


Once i did this now i get the result what i really want:

Here is my output:(i'm not getting xml strings)

{"Cargo":[{"Id":1,"FirstName":"devi","LastName":"priya ","Contactno":"965577796 "},{"Id":2,"FirstName":"arun","LastName":"kumar","Contactno":"9944142109"},{"Id":3,"FirstName":"karu","LastName":"ronald","Contactno":"8883205008"}]}
 
Share this answer
 

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