SqlCommand send '' instead of ' to stored procedure

  • Thread starter Thread starter Bill Struve
  • Start date Start date
B

Bill Struve

You may want to do this, since the first single quote is missing in your
e-mail:

string sqlCounty = "'";

For (int i=0, i<cblCounties.Items.Count)

{

sqlCounty += cblCounties.Items.Value.Trim() + "', '";

}

sqlCounty = sqlCounty.Substring(0,sqlCounty.Length - 3);

I don't know what your stored procedure is, but you may also consider adding
the "(" and ")" here instead of in the stored procedrue:

e.g. Field in @sqlCounty

Indstesd of: Field in (@sqlCounty)

Then the first line would be:

string sqlCounty = "('";

and you would have to add a line at the end:

sqlCounty += ")";

Bill - (e-mail address removed)
 
Let's tidy up Bill's a bit:

StringBuilder sbCounty = new StringBuilder("'", 200); // 200 for
starting capacity chosen mostly at random
foreach(ListITem li in cblCounties.Items)
{
sbCounty.Append(li.Value.Trim());
sbCounty.Append("','");
}

sbCounty.Length -=3; // Just set the length of the StringBuilder, and it
will trim it.

You could use a for/next loop as well, I just think the foreach look
neater. It also only evalueates cblCounties.Items once, instead of twice
per iteration. The difference is probably negligible here, but for some
control collections (like RichTextBox.Lines), it's not.

Also, I don't think that adding the "(" ")" to the parameter will
work -- because they're not part of the parameter, they're part of the SQL
syntax.
--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
Back
Top