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
- namespace WebServiceDemo
- {
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [System.ComponentModel.ToolboxItem(false)]
-
- [System.Web.Script.Services.ScriptService]
- public class WebService1 : System.Web.Services.WebService
- {
- SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnn"].ConnectionString);
-
- [WebMethod]
- [ScriptMethod(UseHttpGet = true, 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;
- }
- }
- }
Web.config
- <configuration>
- <system.web>
- <webServices>
- <protocols>
- <add name="HttpGet"/>
- <add name="HttpPost"/>
- </protocols>
- </webServices>
Data is inserted in table But
I want to return json data when user click on Invoke button?