Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 999
Offer Avatar asked Jan 03 '14 10:01

Offer


1 Answers

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.

like image 179
Xoman5144 Avatar answered Nov 14 '22 23:11

Xoman5144