2015-03-31 401 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"]),这将符合这样的事实,即在你的循环之后,当它只从最后一个循环获取值(即使它正在编译)时,它将访问它。