pipo
pipo

Reputation: 13

How to change selected combobox item in variable

I am busy doing a simple login system in UWP that lets the user select a school to log into. The different schools are listed in the drop down list. Now the problem is, it saves the value that is selected on startup. The value is saved into a variable.

Tried to find the value by index.

    public List<User> GetUsers(string connectionString)
    {
        string campus_selected = ((ComboBoxItem)campuscb.SelectedItem).Content.ToString();

        if (campus_selected=="NewYork")
        {
            const string GetUserQuery = "SELECT username,password,TeacherID from NewYork.UserLogin";

            var users = new ObservableCollection<User>();
            try
            {

                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    if (conn.State == System.Data.ConnectionState.Open)
                    {
                        using (SqlCommand cmd = conn.CreateCommand())
                        {
                            cmd.CommandText = GetUserQuery;
                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    var user = new User();
                                    user.username = reader.GetString(0);
                                    user.password = reader.GetString(1);

                                    users.Add(user);
                                }
                            }
                        }
                    }
                }
                return users.ToList();
            }
            catch (Exception eSql)
            {
                Debug.WriteLine("Exception: " + eSql.Message);
            }
            return null;
        }

        else if (campus_selected == "LA")
        {
            const string GetUserQuery = "SELECT username,password,TeacherID from LA.UserLogin";

            var users = new ObservableCollection<User>();
            try
            {

                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    if (conn.State == System.Data.ConnectionState.Open)
                    {
                        using (SqlCommand cmd = conn.CreateCommand())
                        {
                            cmd.CommandText = GetUserQuery;
                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    var user = new User();
                                    user.username = reader.GetString(0);
                                    user.password = reader.GetString(1);
                                    //user.FacilitatorsID = reader.GetString(2);
                                    users.Add(user);
                                }
                            }
                        }
                    }
                }
                return users.ToList();
            }
            catch (Exception eSql)
            {
                Debug.WriteLine("Exception: " + eSql.Message);
            }
            return null;
        }
        return null;

    }

When the user changes the dropdown list, it should go the corresponding if statement. So if he selects LA, it should execute the code under the LA if statement.

Upvotes: 1

Views: 100

Answers (1)

Mahmudul Hasan
Mahmudul Hasan

Reputation: 828

You need to add a SelectionChanged event handler in your XAML code. Here is a sample code-

XAML

<ComboBox x:Name="myCombo" 
          Header="Fonts" 
          Height="44" 
          Width="296" 
          SelectionChanged="myCombo_SelectionChanged"
          DisplayMemberPath="Source">
    <ComboBoxItem Name="NY">
        <TextBlock Text="New York"/>
    </ComboBoxItem>
    <ComboBoxItem Name="CA">
        <TextBlock Text="California"/>
    </ComboBoxItem>
    <ComboBoxItem Name="DC">
        <TextBlock Text="Washington DC"/>
    </ComboBoxItem>
</ComboBox>
<TextBlock Name="result"/>

C#

    private void myCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (myCombo.SelectedItem == NY)
        {
            result.Text = "You Selected NY";
        }
        else if (myCombo.SelectedItem == DC)
        {
            result.Text = "You Selected DC";
        }
        else if (myCombo.SelectedItem == CA)
        {
            result.Text = "You Selected CA";
        }
    }

Hope I was able to help. :)

Upvotes: 1

Related Questions