1

I have a combobox which is populated by data from an access database using the following code

public void BindComboBox(ComboBox ComboBoxOrg)
{
    con.Open();
    orgload = new OleDbCommand("SELECT organization_id, short_name FROM organization", con);
    OleDbDataAdapter da = new OleDbDataAdapter();
    da.SelectCommand = orgload;
    DataSet ds = new DataSet();
    da.Fill(ds);
    ComboBoxOrg.ItemsSource = ds.Tables[0].DefaultView;
    ComboBoxOrg.DisplayMemberPath = ds.Tables[0].Columns["short_name"].ToString();
    ComboBoxOrg.SelectedValuePath = ds.Tables[0].Columns["organization_id"].ToString();
    con.Close();
}

The XAML UI code is

<ComboBox  x:Name="ComboBoxOrg" 
    Width="308"
    Height="40"
    HorizontalAlignment="Left"
    VerticalAlignment="Top"
    FontSize="18"
    Margin="0,0,0,100" Foreground="#FF666666"
    ItemsSource="{Binding}"/>      

I would like to GET THE SELECTED ITEM then use it to query a table(users) where its id is existing for example.

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:\hcsshare\hcsshare.accdb; Persist Security Info=False");
cmd = new OleDbCommand("SELECT short_name FROM organization WHERE short_name='" + SELECTED_ITEM + "'", con);
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = cmd;
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
if (dataSet.Tables[0].Rows.Count > 0)
{
}

The combobox is getting populated well, SO how do i get the SELECTED_ITEM from the combobox?

jomsk1e
  • 3,585
  • 7
  • 34
  • 59
jayjay
  • 13
  • 8

3 Answers3

1

you can do this way simply:

string value =ComboBoxOrg.SelectedItem.Text();

or

string Value="";
if(ComboBoxOrg.SelectedIndex>=0) 
  Value=((ComboBoxItem)ComboBoxOrg.SelectedItem).Content.ToString();
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • Thanks for your answer @Ehsan Sajjad i tried using your example and i was getting an error "Cannot convert source type 'object' to target type 'string' " to deal with this i had to cast the selected value to string(i.e (string)Combobox.SelectedValue) but during runtime i was also getting another error anyway thanks for your answer i used string selectedorg = ComboBoxOrg.Text; and it worked fine. – jayjay Apr 12 '14 at 12:54
  • you are welcome..i edit in my answer so that it help others with the same problem – Ehsan Sajjad Apr 12 '14 at 12:55
  • ComboBoxOrg.Text returned the SelectedItem Text?? – Ehsan Sajjad Apr 12 '14 at 12:56
  • Yap it did return the SelectedItem text. I am using WPF. – jayjay Apr 13 '14 at 18:20
  • ok i edited my answer, so that others get it right if someone sees for help – Ehsan Sajjad Apr 13 '14 at 18:23
  • @EhsanSajjad When I try `.SelectedItem.Text()`, I get a red squiggly underline under `.Text()` and `'object' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)`. – vapcguy Sep 16 '16 at 14:49
  • @jayjay I tried just using `.Text` as you did and got `""` in my `SelectionChanged()` function - it didn't read the value that would occur after the change - my guess is because the dropdown hasn't updated yet. So it doesn't appear to work in all cases. – vapcguy Sep 16 '16 at 14:51
  • @EhsanSajjad If I try to use the code that casts the `.SelectedItem` to a `ComboBoxItem`, I get `Unable to cast object of type 'System.Data.DataRowView' to type 'System.Windows.Controls.ComboBoxItem'`. – vapcguy Sep 16 '16 at 14:56
0

Your SelectedValuePath is organization_id whereas you are using the selectedValue to match with short_name, change your query condition to WHERE organization_id = selected_value_of_combobox.

Try this, not tested but definitely help you:

DataSet dataSet = new DataSet();
using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:\hcsshare\hcsshare.accdb; Persist Security Info=False"))
{
    con.Open();
    OleDbCommand cmd = new OleDbCommand("SELECT short_name FROM organization WHERE organization_id=?", con);
    cmd.Parameters.Add("?", ComboBoxOrg.SelectedValue);

    using (var adapter = new OleDbDataAdapter(cmd)) 
    {
        adapter.Fill(data);
    }
}
if (dataSet.Tables[0].Rows.Count > 0)
{
    //your code goes....
}

Or if you really need to match the selectedValue with short_name,

use this query:

SELECT short_name FROM organization WHERE short_name=?

And add this as parameter:

cmd.Parameters.Add("?", ((ComboBoxItem)ComboBoxOrg.SelectedItem).Content.ToString());

Read more here.

Community
  • 1
  • 1
jomsk1e
  • 3,585
  • 7
  • 34
  • 59
  • Thanks @jomsk1e i really needed to also know the difference between SelectedItem, SelectedValue and SelectedValuePath this has really helped me alot. I managed to get the selected value using string selectedorg = ComboBoxOrg.Text; now i am trying to get the org id from the organization table then compare it with the org id from the users table before a successful login. I'll appreciate your help on this one. – jayjay Apr 12 '14 at 12:19
0

So I found none of these ways working when I had an event handler on my box of SelectionChanged, unless I did this:

private void MyBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ComboBox cbx = sender as ComboBox;
    string myValue = ((DataRowView)cbx.Items.GetItemAt(cbx.SelectedIndex))).Row.ItemArray[0].ToString();
}

It's ugly as hell, but was the only way I could get the value in this function. The other option, if you do DropDownClosed with EventArgs e, then you can get the .Text of the box, directly. I believe this was because the SelectionChanged event fires prior to actually setting the text, so you have to go back in "manually" to get what item is at the index that was selected, whereas, if you do it after the box has closed, the value has been set. This is a key concept that might be second-nature to many, but not obvious to others.

vapcguy
  • 7,097
  • 1
  • 56
  • 52