2015-07-27 3 views
1

Привет, я прошел через эту ошибку и не могу понять, как ее исправить. У меня есть служба RESTful WCF, пытающаяся извлечь данные из базы данных SQL Server. Благодарю.Ошибка службы WCF RESTful

Error 1 'RestService.RestServiceImpl.GetCompany(string)': not all code paths return a value 

Мои кодирования

RestServiceImpl.svc.cs

public class RestServiceImpl : IRestServiceImpl 
    { 
     public string XMLDATA(string id) 
     { 
      return ("You Requested product" + id); 
     } 
     public string JSONDATA(string id) 
     { 
      return ("You Requested product" + id); 
     }  //ERROR underlined at GetCompany 
     public Company GetCompany(string CompID) 
     { 
      Company comp = new Company(); 
      { 
       SqlConnection con = new SqlConnection(); 
       con.ConnectionString = ""; 
       con.Open(); 
       SqlCommand cmd = new SqlCommand("SELECT COMPANYNAME FROM tblCompany", con); 
       con.Open(); 

       SqlDataReader reader = cmd.ExecuteReader(); 
      } 

     } 
    } 
} 

IRestService.cs

public interface IRestServiceImpl 
    { 
     [OperationContract] 
     [WebInvoke(Method = "GET", 
      ResponseFormat = WebMessageFormat.Xml, 
      BodyStyle = WebMessageBodyStyle.Wrapped, 
      UriTemplate = "xml/{id}")] 
     string XMLDATA(string id); 

     [OperationContract] 
     [WebInvoke(Method = "GET", 
      ResponseFormat = WebMessageFormat.Json, 
      BodyStyle = WebMessageBodyStyle.Wrapped, 
      UriTemplate = "json/{id}")] 
     string JSONDATA(string id); 

     [OperationContract] 
     [WebGet(UriTemplate = "/GetCompany/{CompID}", 
      RequestFormat = WebMessageFormat.Json, 
      ResponseFormat = WebMessageFormat.Json)] 
     Company GetCompany(string CompID); 

    } 

    [DataContract] 
    public class Company 
    { 
     [DataMember] 
     public string CompID { get; set; } 

     [DataMember] 
     public string Company { get; set; } 
    } 
} 

ответ

1

Вы должны изменить способ, чтобы вернуть тип объекта 'компании'

public Company GetCompany(string COMPANYNAME) 
     { 
      Company comp = new Company(); 
      { 
       SqlConnection con = new SqlConnection(); 
       con.ConnectionString = ""; 
       con.Open(); 
       SqlCommand cmd = new SqlCommand("SELECT COMPANYNAME FROM tblCompany", con); 
       con.Open(); 

       SqlDataReader reader = cmd.ExecuteReader(); 
       //Your implementation 
      } 
      return comp ; 
     } 
+0

Ах так глупо спасибо! @Sajeetharan –

+0

@RenaldoGeldenhuis welcome – Sajeetharan

1

Вы должны вернуться в соответствии с вашим способом, который требует объект класса Company: -

public Company GetCompany(string COMPANYNAME) 
      { 
       Company comp = new Company(); 
       { 
        SqlConnection con = new SqlConnection(); 
        con.ConnectionString = ""; 
        con.Open(); 
        SqlCommand cmd = new SqlCommand("SELECT COMPANYNAME FROM tblCompany", con); 
        con.Open(); 

        SqlDataReader reader = cmd.ExecuteReader(); 
       } 

       return comp; 

      } 
Смежные вопросы