Использование двух раскрывающихся списков для выбора заголовков из базы данных.Я могу получить два других для чтения, когда оба выбраны, но когда одно поле списка равно нулю, оно всегда автоматически проходит через последний раздел (RatingListBox.SelectedIndex == -1), даже если это то, что является нулем.
Я хочу иметь возможность выбрать EITHER / OR, а также Both и вернуть окно сообщения, когда ничего не выбрано при нажатии кнопки Search.
private void SearchButton_Click(object sender, RoutedEventArgs e)
{
MovieList.Items.Clear();
string connectionstring = null;
string sql = null;
SqlConnection connection;
SqlCommand command;
SqlDataReader datareader;
//string Genre = GenreListBox?.SelectedValue.ToString();
//string Rating = RatingListBox?.SelectedValue.ToString();
string Director = DirectorTextbox.ToString();
string Actor = ActorTextbox.ToString();
InitializeComponent();
connectionstring = "Data Source=******;" +
"Initial Catalog=***********;" +
"User ID=****************" +
"Password=*******";
if (GenreListBox.SelectedIndex == -1 && RatingListBox.SelectedIndex == -1)
{
string Genre = GenreListBox?.SelectedValue.ToString();
string Rating = RatingListBox?.SelectedValue.ToString();
connection = new SqlConnection(connectionstring);
sql = "SELECT DVD_Title FROM Bluebox_Titles WHERE Genre = '" + Genre + "' AND Rating = '" + Rating + "'";
connection.Open();
command = new SqlCommand(sql, connection);
datareader = command.ExecuteReader();
while (datareader.Read())
{
MovieList.Items.Add(datareader[0]);
}
datareader.Close();
command.Dispose();
connection.Close();
}
else if (GenreListBox.SelectedIndex == -1)
{
string Genre = GenreListBox?.SelectedValue.ToString();
connection = new SqlConnection(connectionstring);
sql = "SELECT DVD_Title FROM Bluebox_Titles WHERE Genre = '" + Genre + "'";
connection.Open();
command = new SqlCommand(sql, connection);
datareader = command.ExecuteReader();
while (datareader.Read())
{
MovieList.Items.Add(datareader[0]);
}
datareader.Close();
command.Dispose();
connection.Close();
}
else if (RatingListBox.SelectedIndex == -1)
{
string Rating = RatingListBox?.SelectedValue.ToString();
connection = new SqlConnection(connectionstring);
sql = "SELECT DVD_Title FROM Bluebox_Titles WHERE Rating = '" + Rating + "'";
connection.Open();
command = new SqlCommand(sql, connection);
datareader = command.ExecuteReader();
while (datareader.Read())
{
MovieList.Items.Add(datareader[0]);
}
datareader.Close();
command.Dispose();
connection.Close();
}
else
{ MessageBox.Show("Please Select Genre And/Or Rating before Searching Titles!"); }