I have a combobox I'm selecting a value from. I want to use the newly selected value in the combobox as a comparison to my database, but I've only been able to return the value that matches with the text that was in the combobox before the selection change.
I've tried changing .text to selected item & selected value, neither of which returns a selection at all.
This is called upon the ComboBox selection change:
private void StationComboBox_1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
GetIP();
}
private void GetIP()
{
string connectionString = "SERVER=localhost;DATABASE=db; UID=PC;Password=pw;";
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand cmd = new MySqlCommand("SELECT IP_stations from stations WHERE stationNumber_stations='" + stationComboBox_1.Text + "'", connection);
connection.Open();
string IP = (string)cmd.ExecuteScalar();
DisplayIP.Text = IP;
This is the code from that ComboBox, which gets its values from the same database, but a different table.
<ComboBox
Grid.Column="1"
x:Name="stationComboBox_1"
FontSize="25"
Width="60"
HorizontalAlignment="Left"
DisplayMemberPath="stationNumber_stations"
ItemsSource="{Binding}"
Text="0"
SelectionChanged="StationComboBox_1_SelectionChanged"
/>
DisplayIP Xaml:
<TextBox x:Name="DisplayIP"/>
Setting up the ComboBoxes:
public void SQLSetup()
{
string connectionString = "SERVER=localhost;DATABASE=db; UID=PC;Password=pw;";
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand cmd = new MySqlCommand("Select stationNumber_stations from stations", connection); //Command to select all the station numbers
connection.Open();
DataTable dt = new DataTable(); //Creates a new data table
dt.Load(cmd.ExecuteReader()); //Loads the data table with the values returned from the MySQL command
connection.Close(); //Closes the MySQL connection
//Sets the values in the station dropdowns to the values from the data table with the station numbers
stationComboBox_1.DataContext = stationComboBox_2.DataContext = stationComboBox_3.DataContext = stationComboBox_4.DataContext = stationComboBox_5.DataContext =
stationComboBox_6.DataContext = stationComboBox_7.DataContext = stationComboBox_8.DataContext = dt;
stationComboBox_1.ItemsSource = stationComboBox_2.ItemsSource = stationComboBox_3.ItemsSource = stationComboBox_4.ItemsSource = stationComboBox_5.ItemsSource =
stationComboBox_6.ItemsSource = stationComboBox_7.ItemsSource = stationComboBox_8.ItemsSource = dt.DefaultView;
//sets an int to the value selected in the station dropdown
string stationSelection_1 = stationComboBox_1.Text;
string stationSelection_2 = stationComboBox_2.Text;
string stationSelection_3 = stationComboBox_3.Text;
string stationSelection_4 = stationComboBox_4.Text;
string stationSelection_5 = stationComboBox_5.Text;
string stationSelection_6 = stationComboBox_6.Text;
string stationSelection_7 = stationComboBox_7.Text;
string stationSelection_8 = stationComboBox_8.Text;
}
I want the DisplayIP TextBox to display the IP of the current selection. However, it is displaying the IP of the previous selection.
Right now, on startup, both the ComboBox & TextBox are empty. I select a station in the ComboBox & the TextBox remains empty. When I select a new station in the ComboBox, the TextBox updates to display the IP address of the first selection.