I figured it out. In my switch statement I have the same thing twice, so the later call was overwriting the earlier call.
The switch is inside a Regex Matches collection:
switch (ColName.ToLower()) { case "": <-- fall through to foreach column SearchKeys.Remove("importance"); dvRowFilter.Append(String.Concat("(", OpenPar)); foreach (string item in SearchKeys) { ColName = item.Replace(" ", "_"); if (dvRowFilter.Length > 0 && !dvRowFilter.ToString().EndsWith("(")) { dvRowFilter.Append(" or "); } switch (ColName.ToLower()) { case "due": FormatDate(ColValue); dvRowFilter.Append(String.Concat("(", ColName, " >= '", StartDate, "' and ", ColName, " < '", EndDate, "')")); break; default: dvRowFilter.Append(String.Concat(ColName, " like '", ColValue, "'")); break; } } dvRowFilter.Append(String.Concat(ClosePar, ")")); break; case "due": FormatDate(ColValue); dvRowFilter.Append(String.Concat("(", ColName, " >= '", StartDate, "' and ", ColName, " < '", EndDate, "')")); break; default: dvRowFilter.Append(ColName + " like '" + ColValue + "'"); break; } dvRowFilter.Append(String.Concat(ClosePar, SqlOp));
Basically the case "": falls into a foreach loop, for every column in a SQL table (looping through a list appending SQL syntax to a StringBuilder). This means that if somebody doesn't specify a column name in the search, it becomes a 'free' search (so falls into case "":) in that every column is checked. The SQL then resembles that in my first post.
But I also want them to be able to do this: "joe bloggs and due: 01/01/2014" which would make it search every column for joe bloggs AND where the date is 01/01/2014. When this specific search is used, the specified date is updating the StartDate and EndDate values, so the free search uses them, which is where the problem lies.
Ideally I would not have the 'case "due":' or 'case default:' twice (they do exactly the same thing), but I don't know of a way to fall from one case in one switch statement to another case in another (outer) switch statement.
The Regex for this is:
(\(*)(?:\s*)(?:(ref|due|description)(?:\s*:\s*))?([A-Za-z0-9.*\/\s]+?)(?:\s*)(\)*)(?:\s*)(\bor|and\b|\z)(?:\s*)
joe bloggs* and (due: 01/01/2014 or due: 31/12/2014) and (company1* or company2*)
This is how the Regex groups then look:
Image may be NSFW.
Clik here to view.