'Compare value stored in query to user's input
How do I get two input values compared to my query records? For example, I enter 500 Double, and I want everyone who has over 500 Double on my list. if I enter 300 Doubles and 150 Triples. Only display everybody who has more than 300 Doubles and more than 150 Triples. Right now, displays the doubles that are more than the input value.
namespace SQLiteIntegration
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (HasValidInput())
{
int doubles = int.Parse(DoublesTextBox.Text);
int triples = int.Parse(TriplesTextBox.Text);
//MARKER - Probably an error here
DataTable dt = new DataTable();
string datasource = @"Data Source=../../lahman2016.sqlite;";
//Batting.'2B' is the number of doubles a player hit in a season
//Batting.'3B' is the number of triples a player hit in a season
string sql = $"SELECT namefirst, namelast, sum(Batting.'2B'), sum(Batting.'3B') from Master JOIN Batting on Master.playerID = Batting.playerID GROUP BY Master.playerid HAVING SUM(Batting.'2B') AND SUM(Batting.'3B') > {doubles};";
using (SQLiteConnection conn = new SQLiteConnection(datasource))
{
conn.Open();
SQLiteDataAdapter da = new SQLiteDataAdapter(sql, conn);
da.Fill(dt);
conn.Close();
}
playerList.Clear();
foreach (DataRow row in dt.Rows)
{
// MARKER: Making something different show up
string playerRow = $"{row[0].ToString()} {row[1].ToString()} - 2B = {row[2].ToString()}, 3B = {row[3].ToString()}";
playerList.Add(playerRow);
}
populateList();
}
}
private List<string> playerList = new List<string>();
private void populateList()
{
ListView.Items.Clear();
foreach (string s in playerList)
{
ListView.Items.Add(s);
}
}
private string ValidateTextBox(TextBox box, string name)
{
string message = "";
string text = box.Text;
int amount = 0;
if (text == "")
box.Text = "0";
else
{
bool isNumber = int.TryParse(text, out amount);
if (!isNumber)
{
message += $"Invalid Input - {name} is not a number! ";
}
if (amount < 0)
{
message += $"Invalid Input - {name} is negative! ";
}
}
return message;
}
private bool HasValidInput()
{
string message = ValidateTextBox(DoublesTextBox, "Doubles") +
ValidateTextBox(TriplesTextBox, "Triples");
if (message == "")
{
return true;
}
else
{
MessageBox.Show(message);
return false;
}
}
}
}
I am expecting if I enter 300 Doubles and 150 Triples. Only display everybody who has more than 300 Doubles and more than 150 Triples. Right now, displays the doubles that are more than the input value.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|