2013-05-16 32 views
-1

我想根據從wcf服務檢索到的數據綁定我的gridview。但它只顯示gridview中的最後一行數據,而不是全部顯示它們。如何將gridview綁定到wcf服務應用程序?

這裏是我的WCF:

try 
{ 
    DSCustomer dscat = new DSCustomer(); 
    //input is EmpUserID 
    cmd.Parameters.AddWithValue("@myuser", id); 
    cmd.CommandText = "mystoredproc"; 
    List<DSCustomer> lst = new List<DSCustomer>(); 
    SqlDataReader dr = cmd.ExecuteReader(); 

    while (dr.Read()) 
    { 
     dscat.MyEmpID = Convert.ToInt32(dr["Emp"]); 
     dscat.MyEmpName = dr["EmpName"].ToString(); 
     dscat.MyUnitName = dr["UnitName"].ToString(); 
     dscat.MyUnitNumber = Convert.ToInt32(dr["Unit"]); 
     dscat.MyRole = dr["Role"].ToString(); 
     dscat.MySurveyStatus = dr["SurveyStatus"].ToString(); 

     //Add all the returns in to the list from back-end 
     lst.Add(dscat); 
    } 

    //returns to the list 
    return lst; 
} 

這是DScustomer

public class DSCustomer 
    { 
     //Created properties based on the count of the data that we want to retrieve 
     public int MyEmpID { get; set; } 
     public string MyEmpName { get; set; } 
     public string MyUnitName { get; set; } 
     public int MyUnitNumber { get; set; } 
     public string MyRole { get; set; } 
     public string MySurveyStatus { get; set; } 

    } 

而且我的Default.aspx:

protected void Button1_Click(object sender, EventArgs e) 
{ 
    MyServiceClient client = new MyServiceClient(); 
    Customer cust = new Customer(); 

    cust = client.getCategori(tbEmpID.Text); 

    var list = new List<Customer> { cust }; 
    GridView1.DataSource=list; 
    GridView1.DataBind(); 
} 
+0

愚蠢的問題。這是服務的真實代碼嗎?我在while循環中沒有看到任何地方實例化任何dscat的新實例。所以這些值每次都會被覆蓋。 – Rich

+1

不要那樣。只是分享你的答案,我沒有分享這個問題聽到你的sl。聲。 – Abdullah

+1

@Abdullah什麼是WCF方法簽名和什麼是'client.getCategori'返回類型? – Damith

回答

0

問題是,我想你調用不同的服務

Customer cust = new Customer(); 
cust = client.getCategori(tbEmpID.Text); // this method only return one customer 
var list = new List<Customer> { cust }; 
GridView1.DataSource=list; 
GridView1.DataBind(); 

在你給定的服務要返回列表,所以你可以直接把它綁定到數據網格

GridView1.DataSource=client.getCategori(tbEmpID.Text).AsEnumerable() ; 
GridView1.DataBind(); 

一兩件事,裏面while循環創建新DSCustomer,並將其添加在最後

while (dr.Read()) 
    { 
     DSCustomer cust = new DSCustomer(); 
     cust.MyEmpID = Convert.ToInt32(dr["Emp"]); 
     cust.MyEmpName = dr["EmpName"].ToString(); 
     cust.MyUnitName = dr["UnitName"].ToString(); 
     cust.MyUnitNumber = Convert.ToInt32(dr["Unit"]); 
     cust.MyRole = dr["Role"].ToString(); 
     cust.MySurveyStatus = dr["SurveyStatus"].ToString(); 
     lst.Add(cust); 
    } 
+0

IT基於empid。當我在後臺運行它時,顯示兩條記錄。 – Abdullah

+0

我試過這種方式,但顯示我數據源是一個無效的類型。它必須是IListSource,IEnumerable或IDataSource。錯誤 – Abdullah

+0

@Abdullah嘗試用'.AsEnumerable()'在末尾 – Damith

0
列出

,你聲明DS陽變量行:

DSCustomer dscat = new DSCustomer(); 

如若WH的內部移動ile循環。雖然您可能會添加N個元素到第1個元素,但第1個元素中的每個DSCustomer元素將與添加到第1個元素列表中的最後一個元素具有相同的值。

另外請注意,您的WCF服務電話:

Customer cust = new Customer(); 
cust = client.getCategori(tbEmpID.Text); 

顯示,你將只能得到1個客戶對象返回(不是很多),然後從該對象創建1項的列表:

var list = new List<Customer> { cust }; // list only has 1 item. 

所以看起來您爲WCF服務展示的代碼與您在客戶端上調用的方法不同。

相關問題