Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9224851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:25:48+00:00 2026-06-18T04:25:48+00:00

I am trying to get currently selected option from a combobox and I’ve tried

  • 0

I am trying to get currently selected option from a combobox and I’ve tried getting it’s text through

ComboBox.Text

Combobox.SelectedItem()

but .Text is returning an empty string and SelectedItem() is returning null

Here is the code on how I am populating the combobox. The combobox value depends on the value of another combobox.

private void cboSite_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
        BackgroundWorker bw = new BackgroundWorker();
        cboPlan.Items.Clear();
        bw.DoWork += new DoWorkEventHandler(bw_cboPlan);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_cboPlanComplete);
        site = cboSite.SelectedItem.ToString();
       Busy.IsBusy = true;
        Busy.BusyContent = "Loading Products";
        bw.RunWorkerAsync();
    }

    void bw_cboPlan(object sender, DoWorkEventArgs e)
    {
        SqlConnection con = new SqlConnection(Class.GetConnectionString());
        SqlCommand scProduct = new SqlCommand("spSelectProduct", con);
        scProduct.Parameters.Add(new SqlParameter("@Site",site));
        scProduct.CommandType = CommandType.StoredProcedure;

        SqlDataReader readerPortal;
        con.Open();

        readerPortal = scProduct.ExecuteReader();

        while (readerPortal.Read())
        {
            this.Dispatcher.Invoke((Action)delegate(){cboPlan.Items.Add(readerPortal[0]);});
        }
        con.Close();
    }

    void bw_cboPlanComplete(object sender, RunWorkerCompletedEventArgs e)
    {
        cboPlan.SelectedIndex = 0;
        Busy.IsBusy = false;
    }

Though I can see the .Text values in the combobox I cannot use them in code.

EDIT: The Null values is returned by the cboPlan Combobox.

And here is when it returns the null in case of SelectedItem() and empty string in case of .Text

if (IsValid())
        {
            BackgroundWorker bw = new BackgroundWorker();
            cboPlan.Items.Clear();
            bw.DoWork += new DoWorkEventHandler(bw_Add);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_AddComplete);
            plan = cboPlan.Text;
            Busy.IsBusy = true;
            Busy.BusyContent = "Sending Request.";
            bw.RunWorkerAsync();
        }

XAML for The Comboboxes.

<ComboBox x:Name="cboSite" HorizontalAlignment="Left" Margin="461,52,0,0" VerticalAlignment="Top" Width="174" SelectionChanged="cboSite_SelectionChanged"/>
<ComboBox x:Name="cboPlan" HorizontalAlignment="Left" Margin="395,106,0,0" VerticalAlignment="Top" Width="240" />
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:25:49+00:00Added an answer on June 18, 2026 at 4:25 am

    Edit

    Well, you have this in your code:

      BackgroundWorker bw = new BackgroundWorker();
      // You will delete all items here!
      cboPlan.Items.Clear();
      bw.DoWork += new DoWorkEventHandler(bw_Add);
      bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_AddComplete);
      // plan is String.Empty because there are no items in the combobox!
      plan = cboPlan.Text;
      Busy.IsBusy = true;
      Busy.BusyContent = "Sending Request.";
      // You will start populating combobox asynchronously here 
      bw.RunWorkerAsync();
    

    I am not sure what are you trying to do, but if you want to save value of the control, do that before calling Items.Clear() function.

    Original answer

    I suggest that you fill List from database in bw_cboPlan, and to fill ComboBox in RunWorkerCompletedEventHandler callback function:

    List<String> combovalues = new List<String>();
    void bw_cboPlan(object sender, DoWorkEventArgs e)
        {
            SqlConnection con = new SqlConnection(Class.GetConnectionString());
            SqlCommand scProduct = new SqlCommand("spSelectProduct", con);
            scProduct.Parameters.Add(new SqlParameter("@Site",site));
            scProduct.CommandType = CommandType.StoredProcedure;
            SqlDataReader readerPortal;
            con.Open();
            readerPortal = scProduct.ExecuteReader();
            combovalues.Clear();
            while (readerPortal.Read())
            {
                 combovalues.Add(readerPortal[0]); // untested
                //this.Dispatcher.Invoke((Action)delegate(){cboPlan.Items.Add(readerPortal[0]);});
            }
            con.Close();
        }
    
        void bw_cboPlanComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            foreach(var item in combovalues)
                cboPlan.Items.Add(item);
            cboPlan.SelectedIndex = 0;
            Busy.IsBusy = false;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to get the currently selected option in a dropdown menu with javascript.
I'm trying to get the currently selected item within an asp:RadioButtonList through the java
I'm currently trying to get the most popular productID from my MSSQL Database. This
Trying to get a list of all substrings currently I have a function but
I'm trying to get not just the value of the option element, but the
I'm trying to get the value currently selected and I simply want to alert
I have a list box and i am trying to get currently checked item
I am currently trying to get my app to run on my iPhone 4.
I am currently trying to get multiple xml files into my xsl file to
I'm currently trying to get into Java Web Development in general in Spring more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.