rahul patil

rahul patil

  • NA
  • 160
  • 8.5k

how to return jsondata using webservice?

May 18 2020 7:15 AM
I want to return json data when filled the webservice page?
 
 
issue is when click on Invoke button Data is not displayed 
 
 

By default the webmethod is post type

WebService1.asmx.cs

  1. namespace WebServiceDemo  
  2. {  
  3.     [WebService(Namespace = "http://tempuri.org/")]  
  4.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  5.     [System.ComponentModel.ToolboxItem(false)]  
  6.     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.   
  7.     [System.Web.Script.Services.ScriptService]  
  8.     public class WebService1 : System.Web.Services.WebService  
  9.     {  
  10.         SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnn"].ConnectionString);  
  11.   
  12.         [WebMethod]  
  13.         [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]  
  14.   
  15.         public string InsertData(string fname, string mname, string lname, string emailid, string password, string contactno, string hobby, string address, string countrycodenum)  
  16.         {  
  17.             cn.Open();  
  18.             string data = "";  
  19.             string insertquery = "insert into tblstudent(fname, mname, lname, emailid, password, contactno,hobby,address,countrycodenum)values(@fname,@mname,@lname,@emailid,@password,@contactno,@hobby,@address,@countrycodenum)";  
  20.   
  21.             SqlCommand cmd = new SqlCommand(insertquery, cn);  
  22.             cmd.CommandType = CommandType.Text;  
  23.             cmd.Parameters.AddWithValue("@fname", fname);  
  24.             cmd.Parameters.AddWithValue("@mname", mname);  
  25.             cmd.Parameters.AddWithValue("@lname", lname);  
  26.             cmd.Parameters.AddWithValue("@emailid", emailid);  
  27.             cmd.Parameters.AddWithValue("@password", password);  
  28.             cmd.Parameters.AddWithValue("@contactno", contactno);  
  29.             cmd.Parameters.AddWithValue("@hobby", hobby);  
  30.             cmd.Parameters.AddWithValue("@address", address);  
  31.             cmd.Parameters.AddWithValue("@countrycodenum", countrycodenum);  
  32.   
  33.             int i = cmd.ExecuteNonQuery();  
  34.             if (i > 0)  
  35.             {  
  36.                 Console.WriteLine("Insert Successfully");  
  37.             }  
  38.             else  
  39.             {  
  40.                 Console.WriteLine("Not Insert Successfully");  
  41.             }  
  42.             cn.Close();  
  43.   
  44.             return data;  
  45.         }  
  46.     }  
  47. }  
Web.config 
  1. <configuration>    
  2.   <system.web>    
  3.    <webServices>    
  4.       <protocols>    
  5.         <add name="HttpGet"/>    
  6.         <add name="HttpPost"/>    
  7.       </protocols>    
  8. </webServices>  
Data is inserted in table But I want to return json data when user click on Invoke button?

Answers (8)