Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HiII'm new to JSON this is my 1st webservice)

I have created an Webservice in asp.net that returns JSON data.Every thin gworkis fine but what i need is when fetching the data from the database it should trim the white spaces before it displays.

Because in my data base i have given the field type as nvarchar(100) but inserted the text with <20 characters so in the output i'm gettin the result with whitespaves also

For eg:
{"Cargo":[{"Id":1,"FirstName":"devi                ","LastName":"priya               ","Contactno":"965577796 "}]}


after the name devi and priya there is lots if spaces.

But the actual result i supposed to get is;

{"Cargo":[{"Id":1,"FirstName":"devi","LastName":"priya ","Contactno":"965577796 "}]}


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, XmlSerializeString = false)]
        //public string GetEmployees(string SearchTerm) 
        
        public void GetEmployees()
        {
            try
            {
                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 serializer.Serialize(rows);
                //return "{ \"Cargo\": " + serializer.Serialize(rows) + "}";
                //JavaScriptSerializer js = new JavaScriptSerializer();
                //string strJSON = js.Serialize(new { Cargo = rows });
                this.Context.Response.ContentType = "application/json; charset=utf-8";
                this.Context.Response.Write(serializer.Serialize(new { Cargo = rows }));
                //return serializer.Serialize(new { Cargo = rows });
                
            }
            catch (Exception ex)
            {
                //return errmsg(ex);
            }
        }
} 


can any one please help me.

thanks in advance.
Posted
Updated 2-May-14 22:02pm
v3

C#
row.Add(col.ColumnName, rs[col].Trim());
will this work?
 
Share this answer
 
Comments
[no name] 4-Apr-14 8:14am    
thanks for your respond will try
[no name] 4-Apr-14 8:15am    
there is no trim in the following code ...
row.Add(col.ColumnName, rs[col].Trim());

is there Any other way???
Rakhesh Rajan 4-Apr-14 8:37am    
row.Add(col.ColumnName, rs[col].tostring().Trim());
You need to add
C#
.Trim() 
in the row
C#
row.Add(col.ColumnName, rs[col]);


Finally it should be like

C#
row.Add(col.ColumnName.Trim, rs[col].ToString().Trim());
 
Share this answer
 
v2

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