Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here I have working code for POST method in WCF REST:-</p> <p>First create database table with id,uname and pwd fields. create a stored procedure to insert values.</p> <pre><code>create procedure [dbo].[sproc_Insertusers] ( @id int out, @uname nvarchar(50), @pwd nvarchar(50) ) as insert into tbl_register ( [uname], [pwd] ) values ( @uname, @pwd ) set @id = @@identity return @id </code></pre> <p>Create new WCF project</p> <p>in IService1.cs</p> <pre><code>[ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "user_register/{uname}/{pwd}")] int user_register(string uname,string pwd); } </code></pre> <p>in Service1.cs</p> <pre><code> public class Service1 : IService1 { SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["iwealth_db"]); SqlCommand cmd; DataSet ds; SqlDataAdapter da; int result; public int user_register(string uname, string pwd) { cmd = new SqlCommand("sproc_Insertusers", cn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@uname",uname); cmd.Parameters.AddWithValue("@pwd", pwd); cmd.Parameters.Add("@id", SqlDbType.Int); cmd.Parameters["@id"].Direction = ParameterDirection.Output;//Output parameter cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); result = (int)(cmd.Parameters["@id"].Value); return result;//returning id } } </code></pre> <p>in web.config:-</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;configuration&gt; &lt;appSettings&gt; &lt;add key="iwealth_db" value="Data Source=localhost;Initial Catalog=iwealth; Trusted_Connection=true"/&gt; &lt;/appSettings&gt; &lt;system.web&gt; &lt;compilation debug="true" targetFramework="4.0" /&gt; &lt;/system.web&gt; &lt;system.serviceModel&gt; &lt;services&gt; &lt;service name="iWealthService.Service1" behaviorConfiguration="ServiceBehaviour"&gt; &lt;!-- Service Endpoints --&gt; &lt;!-- Unless fully qualified, address is relative to base address supplied above --&gt; &lt;endpoint address ="" binding="webHttpBinding" contract="iWealthService.IService1" behaviorConfiguration="web"&gt; &lt;!-- Upon deployment, the following identity element should be removed or replaced to reflect the identity under which the deployed service runs. If removed, WCF will infer an appropriate identity automatically. --&gt; &lt;/endpoint&gt; &lt;/service&gt; &lt;/services&gt; &lt;behaviors&gt; &lt;serviceBehaviors&gt; &lt;behavior name="ServiceBehaviour"&gt; &lt;!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --&gt; &lt;serviceMetadata httpGetEnabled="true"/&gt; &lt;!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --&gt; &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt; &lt;/behavior&gt; &lt;/serviceBehaviors&gt; &lt;endpointBehaviors&gt; &lt;behavior name="web"&gt; &lt;webHttp/&gt; &lt;/behavior&gt; &lt;/endpointBehaviors&gt; &lt;/behaviors&gt; &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt; &lt;/system.serviceModel&gt; &lt;system.webServer&gt; &lt;modules runAllManagedModulesForAllRequests="true"/&gt; &lt;/system.webServer&gt; &lt;/configuration&gt; </code></pre> <p>Build the WCF. Now, the below code is how to use or consume this WCF service</p> <p>Create new website</p> <p>Add register button to test this WCF service</p> <p>and on button click code:-</p> <pre><code> protected void Button1_Click(object sender, EventArgs e) { //in 'sURL' paste WCF service link up to .svc and write -&gt; /user_register/Prateek/1234 //here user_register is service method path and Prateek and 1234 are parameters that will enter to database string sURL = "http://localhost:49271/Service1.svc/user_register/Prateek/1234"; WebRequest wrGETURL; wrGETURL = WebRequest.Create(sURL); wrGETURL.Method = "POST"; wrGETURL.ContentType = @"application/json; charset=utf-8"; HttpWebResponse webresponse = wrGETURL.GetResponse() as HttpWebResponse; Encoding enc = System.Text.Encoding.GetEncoding("utf-8"); // read response stream from response object StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc); // read string from stream data strResult = loResponseStream.ReadToEnd(); // close the stream object loResponseStream.Close(); // close the response object webresponse.Close(); // assign the final result to text box Response.Write(strResult); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload