Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: REST
I want to insert data in SQL database using REST services (JSON response)and I have implemented them.But I couldn't understand how to  insert data without adding any client .Can I insert data directly from service-url without using client app.Because my client application is android and I want to invoke that rest service from android and insert in database..How can I insert data without using client application?
 
My code is :
 
Iservice1.cs
 
 [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(UriTemplate = "Appointments", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        void CreateAppointment(Appointment appointment);
 
    }
 
    [DataContract]
    public class Appointment
    {
        [DataMember]
        public string UserName { get; set; }
        [DataMember]
        public string AppointmentBookedForCompany { get; set; }
        [DataMember]
        public string Date { get; set; }
        [DataMember]
        public string TimeStart { get; set; }
        [DataMember]
        public string TimeEnd { get; set; }
        [DataMember]
        public string PhoneNo { get; set; }
 }
       Service1.cs:
 
  public void CreateAppointment(Appointment appointment)
         {
             string str = "Data Source = . Initial Catalog = Appointment; User ID = sa; Password = 123";
             SqlConnection cn = new SqlConnection(str);
 
             SqlCommand cm = new SqlCommand();
             string username = appointment.UserName;
             string appointmentbookedForcompany = appointment.AppointmentBookedForCompany;
             string date = appointment.Date;
             string timestart = appointment.TimeStart;
             string timeend = appointment.TimeEnd;
             string phoneno = appointment.PhoneNo;
            cm.CommandText = "insert into BookAppointment values(@username,@appointmentbookedForcompany,@date,@timestart,@timeend,@phoneno)";
             cm.Parameters.Add(new SqlParameter("@username", SqlDbType.VarChar,50)).Value = username;
             cm.Parameters.Add(new SqlParameter("@appointmentbookedForcompany", SqlDbType.VarChar,50)).Value = appointmentbookedForcompany;
             cm.Parameters.Add(new SqlParameter("@date", SqlDbType.VarChar,50)).Value = date;
             cm.Parameters.Add(new SqlParameter("@timestart", SqlDbType.VarChar,50)).Value = timestart;
             cm.Parameters.Add(new SqlParameter("@timeend", SqlDbType.VarChar,50)).Value = timeend;
             cm.Parameters.Add(new SqlParameter("@phoneno", SqlDbType.VarChar,50)).Value = phoneno;
 
           cm.Connection = cn;
             cn.Open();
             cm.ExecuteNonQuery();
 
             cn.Close();
 
             cm.Dispose();
         }}
 My config file: <system.serviceModel>
    <services>
      <service name="RESTAppointments.Service1" behaviorConfiguration="RESTAppointments.hk">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/RESTAppointments/Service1/"/>
          </baseAddresses>
        </host>
 
        <endpoint address="http://localhost:8732/Design_Time_Addresses/RESTAppointments/Service1/ed" binding="webHttpBinding" contract="RESTAppointments.IService1" behaviorConfiguration="WebHttpBehavior"/>
 
            <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
 
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="RESTAppointments.hk">
 
          <serviceMetadata httpGetEnabled="True"/>
 
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="WebHttpBehavior">
          <webHttp/>
        </behavior>
        </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  </system.serviceModel>
 
 How can I proceed further ?
Posted 30-Aug-12 19:47pm
cutie1297

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can browse the WCF service and the pass in an xml to save it without a client.
 
You can also use the WCF test client tool - http://msdn.microsoft.com/en-us/library/bb552364.aspx[^].
  Permalink  
Comments
cutie1 at 31-Aug-12 3:11am
   
Hii,,Can u please explain me briefly how to pass in an xml to save it without a client for REST services ..For SOAP services I knew how to do it(invoke procedure).But when I run the services ,I couldn't see the method getting added to the service.When I use basichttpbinding I can see the method.But I dont want to use basichttpbinding since it is a REST service

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 505
1 OriginalGriff 248
2 ChintanShukla 185
3 Richard Deeming 175
4 Bhavik_Patel 170
0 Sergey Alexandrovich Kryukov 8,688
1 OriginalGriff 7,237
2 CPallini 2,598
3 Richard MacCutchan 2,080
4 Abhinav S 1,888


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 31 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100