2015-03-31 402 views
-3

我控制器顯示errror PLS解決示值誤差不能將列表轉換爲字符串無法隱式轉換類型「字符串」到「System.Collections.Generic.List <string>

public JsonResult GetProperty_id(int selectedValue) 
{ 
    List <string> properties = new List <string>().ToList(); 
    // Property model1 = new Property(); 
    SqlConnection con = new SqlConnection("Server=miracle\\SQLExpress; database=CRUIdb; user id=sa; password=dotnet"); 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("SELECT property_id FROM Service_Property WHERE service_id =" + selectedValue, con); 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    DataSet ds = new DataSet(); 
    da.Fill(ds); 

    for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) 
    { 
     properties = (ds.Tables[0].Rows[0]["property_id"]); //error cannot covert 
    } 
    return Json(properties, JsonRequestBehavior.AllowGet); 
} 
+0

@Jenish Rabadiya你可以看看這個問題嗎? http://stackoverflow.com/questions/43122189/onesignal-push-notification-clickevent-show-empty-values-windows-phone-8-1-c-sha – Arsal 2017-04-04 22:51:39

+0

@Arsal這不是我發起這個線程。我剛剛編輯了這個問題,以使其更具可讀性。 – 2017-04-05 03:35:09

回答

2

您正在分配string值到List<string>這是不可能的,而應該添加的每個值到您的收藏像下面

properties.Add(ds.Tables[0].Rows[0]["property_id"]); 
+2

另外,第一行不需要ToList。列表< string > properties = new List < string >().ToList(); – Amit 2015-03-31 10:35:03

2

ds.Tables[0].Rows[0]["property_id"]string但您嘗試設置properties到v ALUE。 propertiesList<string>的聲明。

也許你想要做properties.Add(ds.Tables[0].Rows[0]["property_id"]),這將符合這樣的事實,即在你的循環之後,當它只從最後一個循環獲取值(即使它正在編譯)時,它將訪問它。