in

ASP.NET Weblogs

Dave Burke - A freelance .NET Developer specializing in Online Communities

A freelance .NET Developer

Character parsing code snippet for kicks - updated

I inherited an ASP.NET app that I updated today.  One update was changing a SQL varchar field stored as numbers to letters, as in “1,2,3” to “A,B,C.”  (I'm saying its inherited because I probably wouldn't have stored comma-separated values in a single field had I designed the app.)

As numbers, the following worked fine: “...where code in (1,5,7).“

string sql = "SELECT * FROM table WHERE code IN (" + dr["code"] + ")";

But as letters it spit out an Invalid column name 'G' exception.  I needed to convert the “A,B,C“ string to 'A','B','C'.

This solution isn't clever, and as usual I would bet I get recommendations on how to improve it.  I'm posting it only because it was a 10 minute (at most) coding fix which in the not too distant past would have taken me a lot longer to do.  I can only attribute the decreased coding time to .NET, the economy of the C# syntax, and I'm a lucky cus who gets to write a lot of code.

string sql = "SELECT * FROM table WHERE code IN (" + GetCode(dr["code"].ToString()) + ")";

private string GetCode(string actions)
{
  // actions string = “A, B, C“

 string s = actions.Replace(" ","");
 string m = "";
 string[] arraychars = s.Split(",".ToCharArray());
 for (int i = 0 ; i < arraychars.Length ; i++)
 {
  if (i > 0)
   m += ",'" + arraychars[i].ToString() + "'";
  else
   m += "'" + arraychars[i].ToString() + "'";
 }
 return m;  // returns 'A','B','C'
}
 

[UPDATE]  I knew I'd get improvement suggestions on this.  Now instead of being happy about a 10 minute code fix, I SHOULD have been happy about a 90 second fix!  The updated function now consists of a single line.  Excellent, Jerry!

private string GetActionChars(string actions)
{
 return "'" + String.Join("', '", actions.Replace(" ", "").Split(',')) + "'";
}

Published Feb 24 2004, 11:19 PM by daveburke
Filed under:

Comments

 

Jerry Pisk said:

How about this:

string QuoteListItems(string Actions)
{
return "'" + String.Join("', '", Actions.Replace(" ", "").Split(',')) + "'";
}

A lot faster than a for loop through the array. And of course, SQL injection problem, it seems that nobody who has time to have a blog knows how to write secure code (or doesn't care to).
February 24, 2004 11:44 PM
 

Jerry Pisk said:

More code:

If you know that the items are separated by a comma and a space:

return "'" + Actions.Replace(", ", "', '") + "'";

If the items are separated by just a comma:

return "'" + Actions.Replace(",", "','") + "'";

Or you can use regular expressions to replace a comma and optional whitespace - ",\s*" will be the pattern, replace with "', '" and append "'" at the begining and end of the result.
February 24, 2004 11:55 PM
 

Dave Burke said:

I knew I'd get suggestions for improvement. Thanks, guys.
February 25, 2004 6:35 AM
 

David Cumps said:

Thanks from me as well :) Usefull piece of code
February 25, 2004 8:04 AM
 

TrackBack said:

In the bag tonight: Less bitch'n and whin'n. Counts:Blogging: 8; Dev: 22; Otherwise: 8; SQL: 5; WILY: 8. Line of the night:
February 25, 2004 11:24 PM
 

Enigma said:

why even bother to create a .net function

string sql = "SELECT * FROM table WHERE code IN ( + ''''+replace("+code+",',',''',''')+'''' + ")";
February 26, 2004 1:44 AM
 

Enigma said:

why even bother to create a .net function

string sql = "SELECT * FROM table WHERE code IN ( + ''''+replace("+code+",',',''',''')+'''' + ")";
February 26, 2004 1:54 AM

Leave a Comment

(required)  
(optional)
(required)  
Add