2015-08-03 26 views
0

我已經添加了最喜歡的顏色列表的方法存儲在doTableUsers單個用戶:使用在C#ASP淨列表方法

List<string> colorList = new List<string>(); 

.... 

if (reader.HasRows) 
{ 
    while (reader.Read()) 
    { 
     idColor = reader["idColor"].ToString(); 
     colorList.Add(idColor.ToString()); 
    } 

    ns = ""; 

    foreach (string s in colorList) 
    { 
     if (ns.Length != 0) 
     { 
      ns += ", "; 
     } 
     ns += s; 
    } 
} 

現在我需要顯示它在GridView中爲自己喜愛的單用戶顏色。

如果寫在代碼隱藏在GridView方法:

Response.Write(str.ToString()); 

在輸出時,我的顏色正確的列表:

Antique White, Blueviolet, Dark Blue 

但在查詢中通過了展會的GridView中的參數是逗號,逗號丟失:

Antique WhiteBluevioletDark Blue 

我的代碼如下,提前謝謝。

str = null; 
    strArr = null; 
    count = 0; 

    str = ns == null ? "" : ns.ToString(); 
    char[] splitchar = { ',' }; 

    if (!string.IsNullOrEmpty(str)) 
    { 
     strArr = str.Split(splitchar); 
    } 
    else 
    { 
     strArr = null; 
    } 


    for (count = 0; count <= strArr.Length - 1; count++) 
    { 
     sql = "......"; 
    } 

    DataSet dsProducts = new DataSet(); 

    using (OdbcConnection cn = 
     new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString)) 
    { 
     cn.Open(); 

     using (OdbcCommand cmd = new OdbcCommand(sql, cn)) 
     { 
      for (count = 0; count <= strArr.Length - 1; count++) 
      { 
       cmd.Parameters.AddWithValue("param1", Server.UrlDecode(strArr[count].Trim())); 
       Response.Write(Server.UrlDecode(strArr[count].Trim())); 
      } 

     } 
    } 

回答

0

不回答你的問題,但

ns = ""; 

foreach (string s in colorList) 
{ 
    if (ns.Length != 0) 
    { 
     ns += ", "; 
    } 
    ns += s; 
} 

可以轉換成

var ns = string.Join(", ", colorList.ToArray()); 
+0

@AntonioMailtraq,這是如何解決這個問題在喲你的問題? –

+1

@LarsKristensen我不知道。 – Artiom

0

只是做var newValues = str.Split(',');

+0

我已經這樣做......問題是參數在for循環查詢顯示gridview ... –