2012-01-31 80 views
0

通過jQuery調用此方法並填充下拉框。它已經工作過,直到我需要添加一些額外的信息到下拉文本。我試圖將ID傳入第二個存儲過程,並將該值放在KeyValuePair中的第一個值旁邊。我不知道如何讓這個ID通過。有任何想法嗎?從KeyValuePair獲取值

[WebMethod] 
//public static Dictionary<string, string> LoadRestByCityState(string city, string state) 
public static List<KeyValuePair<string, string>> LoadRestByCityState(string city, string state) 
{ 
    DataSet ds = new DataSet(); 
    Database db = DatabaseFactory.CreateDatabase(ConfigManager.AppSettings["ConnectionString.Data"]); 
    DbCommand dbCommand = db.GetStoredProcCommand("sel_RestByCityState_p"); 
    db.AddInParameter(dbCommand, "@pCity", DbType.String, city); 
    db.AddInParameter(dbCommand, "@pState", DbType.String, state); 
    ds = db.ExecuteDataSet(dbCommand); 

    KeyValuePair<string, string> parks = new KeyValuePair<string, string>(); 

    DataSet dset = new DataSet(); 

    var list = new List<KeyValuePair<string, string>>(); 

    list.Add(new KeyValuePair<string, string>("Select a restaurant", "0")); 
    foreach (DataRow row in ds.Tables[0].Rows) 
    { 
     DbCommand comm = db.GetStoredProcCommand("dbo.sel_RestDetailsByID_p"); 
     db.AddInParameter(comm, "@pRestId", DbType.Int32, //keyvalueid); 
     db.AddInParameter(comm, "@pAttributeCode", DbType.String, "Detail"); 
     dset = db.ExecuteDataSet(comm); 

     string attr = ds.Tables[0].Rows[0]["AttributeValue"].ToString(); 

     list.Add(new KeyValuePair<string, string>(row[0].ToString() + " - " + attr, row[1].ToString())); 

    } 

    return list; 

} 

回答

0

這不是非常清楚你的問題是什麼,但我認爲該行

string attr = ds.Tables[0].Rows[0]["AttributeValue"].ToString(); 

應該讀

string attr = dset.Tables[0].Rows[0]["AttributeValue"].ToString(); 

您正在尋找在外部數據集的數據,而不是設置從存儲過程返回。

任何幫助?

而且,您尚未創建KVP當你調用存儲過程,所以你可以做到這一點

db.AddInParameter(comm, "@pRestId", DbType.Int32, row[0]); // or row[1], it isn't clear to me which you want to use. 
+0

呀不知道這是阻止我或不。讓我編輯我的代碼,看看會發生什麼。 – 2012-01-31 18:00:57

0

嘗試像這可能是它會幫助你的。 您正在Web方法中返回List,Web服務將轉換爲數組。所以你更好地返回Array,以便通過使用ToArray()輕鬆地序列化它; Webservice and List

  1. 使用字典,它會與你的工作KeyValuePair