Offer
Offer

Reputation: 630

Combobox not accurately fetching database values

I'm trying to migrate a windows form app to wpf and I've hit a wall.

It appears when using a combobox, the SelectedIndexChanged event has been replaced by the SelectionChanged as it's wpf equivalant.

My application is connected to a MySQL database where it get's all it's information. A particular combobox of mine is populated by a field in a database table. The idea is; Select a combobox item, other textboxes should show corresponding values of the same row. Instead this is what happens.

Example

the code behind:

    private void Domain_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // Set connection parameters
        string sqlcon = "datasource = localhost; port = 3306; username = root; password = localhost";
        // Set query to excecute
        string query_fetch = "select * from mail.smtp where domain = '" + this.Domain.Text + "';";
        // declaratons
        MySqlConnection con = new MySqlConnection(sqlcon);
        MySqlCommand cmd_fetch = new MySqlCommand(query_fetch, con);
        MySqlDataReader rdr;

        // Excecution of command
        try
        {
            con.Open();
            rdr = cmd_fetch.ExecuteReader();

            while (rdr.Read())
            {
                // Declarations
                string sdomainid = rdr.GetInt32("domainid").ToString();
                string ssmtp = rdr.GetString("smtp");
                string sport = rdr.GetString("port");
                string ssecurity = rdr.GetString("security");

                // Bindings
                Domain_ID.Text = sdomainid;
                SMTP.Text = ssmtp;
                port.Text = sport;
                security.Text = ssecurity;
            }
            con.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

The xaml:

    <ComboBox x:Name="Domain" SelectedValue="-1" Margin="186,132,0,0" Height="18" VerticalAlignment="Top" HorizontalAlignment="Left" Width="211" FontSize="11" SelectionChanged="Domain_SelectionChanged" TabIndex="4">

It works the way it's supposed to with the SelectedIndexChanged event in wWinForms though. I just cant seem to figure out a way to traslate it properly in wpf. Any help is much appreciated. (kindly ignore the "stmp" typo)

Upvotes: 8

Views: 695

Answers (3)

If you populate your ComboBox from database correctly, you don't need any SelectionChanged event. You can bind the fields of the selected item of your ComboBox to the text boxes like this:

<ComboBox x:Name="DomainCB" ItemsSource="{Binding}" />

Text boxes:

<TextBox Text="{Binding Path=SelectedItem.domainid, ElementName=DomainCB}" />
<TextBox Text="{Binding Path=SelectedItem.smtp, ElementName=DomainCB}" />
<TextBox Text="{Binding Path=SelectedItem.port, ElementName=DomainCB}" />
<TextBox Text="{Binding Path=SelectedItem.security, ElementName=DomainCB}" />

Upvotes: 0

Xoman5144
Xoman5144

Reputation: 33

I am not sure whether this would help or not because everyone is showing you the code for the problem, but in my experience, if you using data sets you can fix this problem really easily. if you use the data source tab you can use it to create a data set of all the tables you want to use. Then you find the table in the data set and drag and drop the properties you want to use into the combobox. if done correctly there should be no problem.

Upvotes: 1

Jamie Clayton
Jamie Clayton

Reputation: 1017

I would check your XAML bindings for the control. Review your settings for UpdateSourceTrigger to ensure the event flows down to your event handlers.

<ListView x:Name="RolesListView"
   ItemsSource="{Binding Path=RoleOptionListMember, 
                         ValidatesOnDataErrors=True, 
                         UpdateSourceTrigger=PropertyChanged}"
   SelectionMode="Single"
   ScrollViewer.VerticalScrollBarVisibility="Auto"
   Focusable="True">

Upvotes: 0

Related Questions