Skip to content

Instantly share code, notes, and snippets.

@Ryanb58
Created March 12, 2015 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ryanb58/12f6a72d19dbfa0c86b0 to your computer and use it in GitHub Desktop.
Save Ryanb58/12f6a72d19dbfa0c86b0 to your computer and use it in GitHub Desktop.
Simple function that builds a row filter query via the parameters in the querystring. Checks to ensure that the key matches a column in the dataviewer.
private void getSearchQueryFromQueryString()
{
try
{
//String for row query.
StringBuilder sb = new StringBuilder();
//Get all the information in the query strings.
NameValueCollection querystrings = Request.QueryString;
//Is there is any parameters provided?
if (querystrings.HasKeys())
{
//Loop through each.
foreach(string key in querystrings.Keys)
{
string value = querystrings[key];
bool columnExistInData = false;
//Does this key match a column in the data viewer?
foreach (DataColumn dc in dv.Table.Columns)
{
if (dc.ColumnName.ToString().Trim().ToLower() == key.Trim().ToLower())
{
//Match!
columnExistInData = true;
break; //Get out of the loop.
}
}
//Make sure both are clean and exist.
if (!String.IsNullOrEmpty(key) && !String.IsNullOrEmpty(value) && columnExistInData)
{
//If they both have value to them. Check if the value is a string or int.
int result;
bool isInt = int.TryParse(value, out result);
//Check if we need a transition word.
if (sb.Length > 0)
{
sb.Append(" AND ");
}
//Is it an integer or string?
if (isInt)
{
//Value is a INT.
sb.Append("[" + key + "] = " + value);
}
else
{
//Value is a string.
sb.Append("[" + key + "] like '*" + value + "*'");
}
}
}
//set the filter. Make dv.RowFilter = filterQuery later on in code after calling this function.
filterQuery = sb.ToString();
}
}
catch (Exception ex)
{
//Something went wrong.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment