0

I have this line of code:

sql += " AND lc.name IN ('" + String.Join(",", id.type.ToArray()) + "')";

There are two items in id.type and this code generates this:

AND lc.name IN ('towns back to back,towns 3 storey')

Which will not work because it should be like this:

AND lc.name IN ('towns back to back' , 'towns 3 storey')

How can I fix this?

user979331
  • 11,039
  • 73
  • 223
  • 418

2 Answers2

0

This is not a desirable approach, because it's open to SQL injection. But there are a few things:

  1. You have to accommodate for strings that contain single quotes.
  2. It won't work if you just concatenate. You have to wrap EACH item in single quotes.

Try:

sql += " AND lc.name IN (" + String.Join(",", id.type.ToArray().Select(i=>String.Format(i.Replace("'","''"),"'{0}'")) + ")";
Xavier J
  • 4,326
  • 1
  • 14
  • 25
-1

Use parameter to pass input string like

sql += " AND lc.name IN (@inputname)";
sqlcommand.parameters.AddWithValue("@inputname", String.Join("','", id.type.ToArray()))
Ram
  • 504
  • 3
  • 11