2012-06-06 63 views
0

我有下面的代碼和Web方法:得到錯誤「您必須實現上的System.Array默認訪問」

public class RaumklassenHelper 
{ 
    internal static Array Raumklasse() 
    { 
     List<object> raumKlassenObject = new List<object>(); 

     using (SqlConnection con = new SqlConnection(@"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT-V2;Integrated Security=true;")) 
     using (SqlCommand cmd = new SqlCommand(@"SELECT BEZEICHNUNG, BEZEICHNUNG_EN FROM RAUMKLASSE", con)) 
     { 
      con.Open(); 
      using (SqlDataReader rdr = cmd.ExecuteReader()) 
      { 
       while (rdr.Read()) 
       { 
        if (rdr["BEZEICHNUNG"] != DBNull.Value && rdr["BEZEICHNUNG_EN"] != DBNull.Value) 
        { 
         raumKlassenObject.Add(new 
          { 
           Name = rdr["BEZEICHNUNG"].ToString(), 
           Name_En = rdr["BEZEICHNUNG_EN"].ToString() 
          }); 
        } 
       } 
      } 
     } 
     return raumKlassenObject.ToArray(); 
    } 
} 



[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    [WebMethod] 
    public Array Raumklasse() 
    { 
     return RaumklassenHelper.Raumklasse(); 
    } 

蔭得到一個錯誤,如果我嘗試調用的方法:

這是第一次出現錯誤。

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: &lt;&gt;f__AnonymousType0`2[System.String,System.String] cannot be serialized because it does not have a parameterless constructor. 
    at System.Xml.Serialization.TypeDesc.CheckSupported() 
    at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError) 
    at System.Xml.Serialization.XmlSerializationWriter.CreateUnknownTypeException(Type type) 
    at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType) 
    at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType) 
    at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write6_ArrayOfAnyType(Object o) 
    at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer) 
    at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) 
    --- End of inner exception stack trace --- 
    at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) 
    at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces) 
    at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o) 
    at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue) 
    at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream) 
    at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues) 
    at System.Web.Services.Protocols.WebServiceHandler.Invoke() 

如何解決這個問題?我不知道這個錯誤來自

THX進來Advace

回答

5

嘗試使用對象[]作爲返回類型,而不是陣列


的另一個問題:

public class RaumklassenHelper 
{ 

     public class RAUMKLASSE 
     { 
      public string Name { get; set; } 
      public string Name_En { get; set; } 
     } 


     internal static List<RAUMKLASSE> Raumklasse() 
    { 
     List<RAUMKLASSE> raumKlassenObject = new List<RAUMKLASSE>(); 

     using (SqlConnection con = new SqlConnection(@"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT-V2;Integrated Security=true;")) 
     using (SqlCommand cmd = new SqlCommand(@"SELECT BEZEICHNUNG, BEZEICHNUNG_EN FROM RAUMKLASSE", con)) 
     { 
      con.Open(); 
      using (SqlDataReader rdr = cmd.ExecuteReader()) 
      { 
       while (rdr.Read()) 
       { 
        if (rdr["BEZEICHNUNG"] != DBNull.Value && rdr["BEZEICHNUNG_EN"] != DBNull.Value) 
        { 
         raumKlassenObject.Add(new RAUMKLASSE() 
          { 
           Name = rdr["BEZEICHNUNG"].ToString(), 
           Name_En = rdr["BEZEICHNUNG_EN"].ToString() 
          }); 
        } 
       } 
      } 
     } 
     return raumKlassenObject; 
    } 
} 



[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    [WebMethod] 
    public List<RAUMKLASSE> Raumklasse() 
    { 
     return RaumklassenHelper.Raumklasse(); 
    } 
+0

THX,這解決了我的問題! :) – Bashud

+0

你歡迎:) –

+0

嘿艾哈邁德你能幫助我多一次。現在,如果iam試圖調用我的Webmethod我得到一個System.InvalidOperationException。我更新了錯誤的問題 – Bashud

相關問題