Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to return json data when filled the webservice page?




public class WebService1 : System.Web.Services.WebService
    {
        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnn"].ConnectionString);
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string InsertData(string fname, string mname, string lname, string emailid, string password, string contactno, string hobby, string address, string countrycodenum)
        {
            cn.Open();
            string data = "";
            string insertquery = "insert into tblstudent(fname, mname, lname, emailid, password, contactno,hobby,address,countrycodenum)values(@fname,@mname,@lname,@emailid,@password,@contactno,@hobby,@address,@countrycodenum)";

            SqlCommand cmd = new SqlCommand(insertquery, cn);
            //cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@fname", fname);
            cmd.Parameters.AddWithValue("@mname", mname);
            cmd.Parameters.AddWithValue("@lname", lname);
            cmd.Parameters.AddWithValue("@emailid", emailid);
            cmd.Parameters.AddWithValue("@password", password);
            cmd.Parameters.AddWithValue("@contactno", contactno);
            cmd.Parameters.AddWithValue("@hobby", hobby);
            cmd.Parameters.AddWithValue("@address", address);
            cmd.Parameters.AddWithValue("@countrycodenum", countrycodenum);

            int i = cmd.ExecuteNonQuery();
            if (i > 0)
            {
                Console.WriteLine("Insert Successfully");
            }
            else
            {
                Console.WriteLine("Not Insert Successfully");
            }
            cn.Close();

            return data;
        }
    }
}
x

I add this line for return the json data


[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

when I inserted the data using webservice and click the Invoke Button of the webservice page then data is not display?


What I have tried:

I add this line for return the json data but json data is not displayed

[ScriptMethod(ResponseFormat = ResponseFormat.Json)
Posted
Updated 19-May-20 5:11am
Comments
Richard Deeming 19-May-20 8:16am    

The method that you have declared is setup to return a string.
The method also defines a string called data, and is populated as an empty string.
The end of routine does a return of this same data variable.

The problem is... data is never defined as anything but an empty string.
 
Share this answer
 
Comments
Member 14836995 19-May-20 8:28am    
hi MadMyche

string data = fname + mname + lname + emailid + password + contactno + hobby +
address + countrycodenum;

watch window

data "rajumakvanadineshrahulaxay@gmail.com123451234567890cricketsuratuzbekistan" string


but issue is not return the proper data
MadMyche 19-May-20 8:44am    
That is the data that you are passing into the method; and not the value of the data variable you have declared, defined, and returned within it
string data = "";  // line  9

return data;       // line 35
Member 14836995 19-May-20 8:54am    
Hi MadMyche

can u give any solution for that and how to solve this issue

I want to convert string data object to json data object
MadMyche 19-May-20 10:55am    
I would do this pretty close to an MVC structure:
1. Create a Student class with the properties.
2. Populate this class with incoming values to create object instance.
3. Save the object to the DB
4. Use the JSON Serializer to return the object
C#
[System.Web.Script.Services.ScriptService]
public class WebService1 : WebService
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void InsertData(string fname, string mname, string lname, string emailid, string password, string contactno, string hobby, string address, string countrycodenum)
    {
        // create anonymous type with the data
        var data = new { fname, mname, lname, emailid, password, contactno, hobby, address, countrycodenum };

        // serialise that data to JSON
        string json = (new System.Web.Script.Serialization.JavaScriptSerializer()).Serialize(data);

        // write it to the response stream
        Context.Response.Write(json);
    }
}
 
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