2016-03-05 74 views
0

我已經嘗試修復代碼將從SQL數據庫得到蔚藍的推廣數據(表名是促銷是由ID的,PromoName,...等)連接Azure在UWP

我米嘗試使用WCF服務通過創建這些代碼

public interface IService1 
{ 

    [OperationContract] 
    string GetData(int value); 

    [OperationContract] 
    ObservableCollection<PromotionList> GetPromotionList(); 

    [OperationContract] 
    CompositeType GetDataUsingDataContract(CompositeType composite); 
} 

這對Service1.svc.cs

public ObservableCollection<PromotionList> GetPromotionList() 
    { 
     ObservableCollection<PromotionList> result = new ObservableCollection<PromotionList>(); 
     //Add data string here 
     SqlConnection conn = new SqlConnection(); 
     try 
     { 
      conn.ConnectionString = //This one is ado one that copy from the azure 
      conn.Open(); 
      SqlCommand cmd = new SqlCommand(); 
      cmd.Connection = conn; 
      //Query Here 
      String cmdText = "Select * from Promo"; 
      cmd.CommandText = cmdText; 
      cmd.CommandType = CommandType.Text; 
      SqlDataReader dr = cmd.ExecuteReader(); 
      while(dr.Read()) 
      { 
       result.Add(new PromotionList(){id=dr.GetInt32(0),PromoName=dr.GetString(1)}); 
      } 

     } 
     catch(Exception) 
     { 
      throw; 
     } 
     finally 
     { 
      if(conn.State==ConnectionState.Closed) 
      { 
       conn.Close(); 
      } 

     } 
     return result; 
    } 

真正在宣傳片表中的數據進行有更詳細,但我選擇只有2

public class PromotionList 
{ 
    public int id { get; set; } 
    public string PromoName { get; set; } 
} 

我使用這項服務來我的主要應用程序的服務引用,並調用它

(In method public sealed partial class MainPage : Page) 

public ObservableCollection<Connector.PromotionList> PromoList { get; set; } 
.... 
(In method public MainPage()) 
PromoList = new ObservableCollection<Connector.PromotionList>(); 
.... 
(In method When the button is click) 
Connector.Service1Client client = new Service1Client(); 
PromoList = client.GetPromotionListAsync().Result; 
Debug.WriteLine(PromoList[0].PromoName); 

它不會例外作品「名‘InnerExceptionCount’並不在當前的背景下存在」

我想知道這是爲什麼發生異常,以及如何解決它

謝謝

+0

如果您的客戶端應用程序或Web服務發生異常,您可以共享嗎? – Daredevil

+0

當我在設備上運行應用程序時,它在「PromoList = client.GetPromotionListAsync()。」結果;「 –

回答

0

來自wcf服務的所有錯誤都會拋出此異常。 從您的代碼中,連接字符串錯誤或表格促銷不存在可能會發生此錯誤。因此請檢查Service1.svc.cs中的WCF服務代碼。 我已經用正確的連接字符串和右表測試了這些代碼,它運行良好。