I'm trying to write a custom query generator for a small database that I'm making, but the comma that should appear in between all the entries to the string aren't appearing only the one at the end is.
private void BTN_advancedSearch_Click(object sender, EventArgs e)
{
// Creates the variable part of the custom query
string customwhereclause = "";
if (CHK_enableGameName.Checked == true)
{
Connectqry(customwhereclause);
customwhereclause += "Game.GameName LIKE '%" + TXT_addGame.Text + "%'";
}
if (CHK_enableGenreName.Checked == true)
{
Connectqry(customwhereclause);
customwhereclause += "Genre.GenreID =" + genreID + "";
}
if (CHK_enableConsoleName.Checked == true)
{
Connectqry(customwhereclause);
customwhereclause += "Console.ConsoleID =" + consoleID + "";
}
if (CHK_enablePlayers.Checked == true)
{
Connectqry(customwhereclause);
customwhereclause += "Game.Players >=" + NUD_players.Value + "";
}
if (CHK_enableDisc.Checked == true)
{
if (CHK_discOwned.Checked == true)
{
Connectqry(customwhereclause);
customwhereclause += "Game.Disc ='" + "yes" + "'";
}
else
{
Connectqry(customwhereclause);
customwhereclause += "Game.Disc ='" + "no" + "'";
}
}
if (CHK_enableCompleted.Checked == true)
{
if (CHK_completed.Checked == true)
{
Connectqry(customwhereclause);
customwhereclause += "Game.Completed ='" + "yes" + "'";
}
else
{
Connectqry(customwhereclause);
customwhereclause += "Game.Completed ='" + "no" + "'";
}
}
//varible query code being passed back to search form.
frm_search.Cstmqry = customwhereclause;
//close the form and reopen the other one.
this.Close();
frm_search.Show();
}
private void Connectqry(string s)
{
if (s == "")
{
Console.WriteLine("the query is blank");
}
else
{
s = s + " , ";
Console.WriteLine(s);
}
}
the output is currently this:
the query is blank
Game.GameName LIKE '%name%' ,
Game.GameName LIKE '%name%'Genre.GenreID =0 ,
Game.GameName LIKE '%name%'Genre.GenreID =0Console.ConsoleID =0 ,
Game.GameName LIKE '%name%'Genre.GenreID =0Console.ConsoleID =0Game.Players >=1 ,
Game.GameName LIKE '%name%'Genre.GenreID =0Console.ConsoleID =0Game.Players >=1Game.Disc ='no' ,
I'm not sure why it's removing the commas that be in between the string.
You should add the code:
if (!string.IsNullOrEmpty(customwhereclause))
{
customwhereclause += " AND ";
}
customwhereclause += // Your condition
in all your conditions. It'll add an AND
operator everywhere it's necessary.
Even better:
private static string computeCondition(string current, string newCondition)
{
if (!string.IsNullOrEmpty(current))
{
current += " AND ";
}
return current + newCondition;
}
private void BTN_advancedSearch_Click(object sender, EventArgs e)
{
// Creates the variable part of the custom query
string customwhereclause = "";
if (CHK_enableGameName.Checked == true)
{
Connectqry(customwhereclause);
customwhereclause = computeCondition(customwhereclause, "Game.GameName LIKE '%" + TXT_addGame.Text + "%'");
}
...
To avoid too big code dup
Or even better:
private void BTN_advancedSearch_Click(object sender, EventArgs e)
{
// Creates the variable part of the custom query
List<string> whereClausesList = new List<string>();
if (CHK_enableGameName.Checked == true)
{
Connectqry(customwhereclause);
whereClausesList.Add("Game.GameName LIKE '%" + TXT_addGame.Text + "%'");
}
...
string.Join(" AND ", whereClausesList);
as suggested by Rob