Answered REST services to insert data in two classes?

  • Saturday, September 22, 2012 12:26 PM
     
     

    Hii

    I 'm creating a REST service in which I want to insert data in SQL .But my problem is I have some of the parameters to insert in one class and some of the parameters in other class

    [DataContract]

    public class Customer

    {

    [DataMember]

    public string CustomerName{get;set;}

    [DataMember]

    public string CustomerId{get;set;}

    }

    [DataContract]

    public class Employee{

    [DataMember]

    public string EmployeeName{get;set;}

    [DataMember]

    public string EmployeeId{get;set;}

    }

    Now I want to insert these 4 parameters(CustomerName,CustomerId,EmployeeName,EmployeeId) into one SQL table(i.e Details).

    I couldnot understand how to construct webinvoke attribute and method and post the data to cRESTclient. 

    any help would be appreciated....

     

All Replies

  • Saturday, September 22, 2012 2:05 PM
     
      Has Code

    I don't think it is possible to have two input parameters to a method that is going to be invoked using an HTTP POST request. You can create a composite type and enclose the Customer and Employee Types within it like this - 

    class SomeClass
    {
        public Customer CustomerInfo {get; set;}
        public Employee EmployeeInfo {get; set;}
    
    }


    Please "Mark as Answer" if a post has answered your question or "Vote as Helpful" if it was helpful in some way. Here's why

  • Sunday, September 23, 2012 6:14 AM
     
      Has Code

    Actually in my project I have something like this:I have a class 'Information'.In this 'Information' class I have another class 'Employee'..and in this 'Employee' i have a class 'Customer'

    public class Information
    
    {
    
    public Employee EmployeeInfo{get;set;}
    
    }
    
    public class Employee
    
    {
    
    public Customer CustomerInfo{get;set;}
    
    public string EmployeeName{get;set;}
    
    public string EmployeeId{get;set;}
    
    }
    
    public class Customer
    
    {
    
    public string CustomerName{get;set;}
    
    public string CustomerId{get;set;}
    
    }


    And i want to insert data in the fields of this class..This is my requirement..How can I do it?


    • Edited by haji1 Sunday, September 23, 2012 6:15 AM
    •  
  • Sunday, September 23, 2012 12:27 PM
     
     

    Then why don't you simply pass the "Information" class as input to the web service?



    Please "Mark as Answer" if a post has answered your question or "Vote as Helpful" if it was helpful in some way. Here's why

  • Sunday, September 23, 2012 3:10 PM
     
      Has Code

    I am writing like this :

     [OperationContract]
            [WebInvoke(UriTemplate = "Info", Method = "POST")]
            void BoApp(Information information);


    and in Service.cs:

     
    public void BoApp(Information information)
     {
       Employee employeeInfo=new Employee();
       Customer customerInfo=new Customer();
       string s="insert into details    (EmployeeName,EmployeeId,CustomerName,CustomerId)   values('"+information.employeeInfo.EmployeeName+"','"+information.employeeInfo.EmployeeId+"','"+'"+information.employeeInfo.customerInfo.CustomerName+"','"+information.employeeInfo.customerInfo.CustomerId+"')";
       SqlCommand cm = new SqlCommand(s1, cn);
                    cn.Open();
                    cm.ExecuteNonQuery();
                    cn.Close();
     }

    And I am testing this with cREST client of Chrome Browser, but when I post the request to Information class then I am getting all the values as null eventhough I am passing values.

    I couldn't understand where I m I going wrong and how should I check it with any client ?


    • Edited by haji1 Sunday, September 23, 2012 3:10 PM
    •  
  • Monday, September 24, 2012 12:17 PM
     
     Answered Has Code

    Hi,

    I can see that in your code you are creating new instances of Employee and Customer objects and hence you are getting null values for EmployeeId and CustomerId.

    You should change your code to 

    Employee employeeInfo=information.EmployeeInfo;
    Customer customerInfo=information.CustomerInfo;


    Please "Mark as Answer" if a post has answered your question or "Vote as Helpful" if it was helpful in some way. Here's why