Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
can any one please help me on this.
C#
for (int i = 0; i < gridview1.Rows.Count; i++)
{
   string Emp2 = ((TextBox)gridview1.Rows[i].FindControl("TxtEmp")).Text;
   histList.Add(Emp2.ToString());
}
String EmpArrayCsv = String.Join("','", histList.ToArray());

here in EmpArrayCsv variable i have added the ("','") quotes and concating the list values but the first and the last variable are not closed in single quotes. is there any other way to do this or how can i rectify it? this values i have to send to sql to update the table where it requires the values to be in comma separated and inside single quotes.
Posted
Updated 22-May-12 9:25am
v3
Comments
manashsatpathy 22-May-12 8:16am    
Thanks deepti..............

You will have to add the single before and after the result string explicitly as the String.Join function will concatenate the values in array by putting the string ',' in between the values but it will not put single quote before the very fist value and very last value. So after your code append one more statement:

C#
EmpArrayCsv = String.Concat("'",EmpArrayCsv,"'");

Hope this helps.
 
Share this answer
 
v2
Comments
fjdiewornncalwe 22-May-12 15:26pm    
+5. Simple and clean.
The following code can be used to concatenate the items with , and enclosing each item with single quotes

C#
List<string> histList = new List<string>
{"one","two","three"};

//Using LINQ
string EmpArrayCsv = histList.Aggregate ("",(string agg, string it) =>
    agg +=string.Format("{0} '{1}'", agg == "" ? "" : ",", it ));
Console.WriteLine (EmpArrayCsv);

//Using foreach loop
string EmpArrayCsv2 = "";
foreach(string item in histList)
    EmpArrayCsv2 += string.Format("{0} '{1}'", EmpArrayCsv2 == "" ? "" : ",", item );
Console.WriteLine (EmpArrayCsv2);
//EmpArrayCsv
//'one', 'two', 'three'
//EmpArrayCsv2
// 'one', 'two', 'three'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900