2013-04-23 29 views
0

**我創建了一個web服務來檢索特定公司代碼的所有者列表。 當我運行這個webservice ...它工作正常。 請幫助我執行此與Web應用程序,因爲當我做同樣的結果顯示爲「SYSTEM.TOSTRING []」
該Web應用程序包括一個文本框輸入公司代碼和一個按鈕來獲取OWNERNAME在下拉列表webservice使用C#從數據庫中檢索數據

public class OwnerWebService : System.Web.Services.WebService 
{  
      string ConnectionString = "Data Source=localdb" + ";" + "Initial Catalog=Prod" + ";" + 
      "Persist Security Info=True" + ";" + 
      "User ID=User1" + ";" + 
      "Password=User1" + ";" + 
      "enlist=false"; 

[WebMethod] 
    public string[] dtFetch(string strCompanyCode) 
    { 
     List<string> messages = new List<string>(); 
     SqlConnection cnMySQL = new SqlConnection(ConnectionString); 
     cnMySQL.Open(); 
     string sqlquery = string.Format("SELECT OwnerName FROM tbl_Owner WHERE CompanyCode='{0}'", strCompanyCode); 
     SqlCommand com = new SqlCommand(sqlquery, cnMySQL); 
     SqlDataReader sqlReader = com.ExecuteReader(); 

     while (sqlReader.Read()) 
     { 
      messages.Add(sqlReader.GetString(0)); 
     } 


     cnMySQL.Close(); 
     return messages.ToArray(); 

    } 

}

+0

我認爲你的代理不是最新的。 – 2013-04-23 05:48:35

+1

它的重複問題, http://stackoverflow.com/questions/12146723/system-string-returned-instead-of-array – 2013-04-23 05:49:19

回答

0

不返回一個字符串[] 而是試圖返回一個逗號分隔的字符串。

返回一個逗號分隔字符串修改的最後一行從

return messages.ToArray(); 

return String.Join(',',messages.ToArray()); 

返回類型也更改爲string 希望它可以幫助...

0

你不能將一組字符串(字符串[])放到一個單獨的文本框中,該文本框只能顯示一個字符串。

您的webservice很好:它返回所有者名稱的集合。所以用戶界面必須被修改。

有幾種解決方案:

  1. 在你的用戶界面,concatinate所有字符串合併爲一個字符串(的string.join( 「」,消息))。不要在你的web服務中這樣做!不應該允許webserivce進行UI格式化!
  2. 在您的用戶界面中,將文本框替換爲列表框,顯示您可以在該列表框中顯示多個所有者名稱。

最後一個(離題)建議:對SqlConnection和SqlDataReader使用關鍵字「using」。這確保了即使發生異常也會關閉它們。