How to show success message in JSON format after saving data using Web-service in C#?

I am trying to save data into database using web service[asmx], after saves successfully data into table I want to show successful message in JSON format 

Here is my code:


//[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 

[ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)]

public bool rating(string email, string name,string age,string gender)


HttpResponse response = HttpContext.Current.Response;

System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

string cs = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
int rowsInserted = 0;

bool t = false;

SqlConnection conn = new SqlConnection(cs);


string sql = "insert into st_data values('" + email+ "','" + name+ "','"+ age+"','" + gender+ " ')";

SqlCommand cmd= new SqlCommand(sql, conn);
int i = cmd.ExecuteNonQuery();

if(i > 0)

// string strJSON = js.Serialize(t);

t = true;

return t;


I am Getting output message like this

<boolean xmlns="">true</boolean>

I want it to be in JSON format  like 


 How can I achieve it?

Asked by:- RameshwarLate
: 5839 At:- 10/7/2017 9:15:39 AM
asmx c# web-service return-JSON-format-from-asmx

1 Answers
profileImage Answered by:- Sam

You can return JSON data from asmx using POST only

Make sure that the request is a POST request, not a GET. Scott Guthrie has a post explaining why.
Though it's written specifically for jQuery, this may also be useful to you:
Using jQuery to Consume ASP.NET JSON Web Services


After that you can  change last line of code as

return  new JavaScriptSerializer().Serialize(YourObj);  

refer these links to learn about Web service in ASP.NET

  3. (How to create JSON web service in
At:- 10/7/2017 10:27:02 AM Updated at:- 10/7/2017 10:29:20 AM

Login/Register to answer
Register directly by posting answer/details

Full Name *

Email *

By posting your answer you agree on privacy policy & terms of use