Link to home
Start Free TrialLog in
Avatar of MichelleLacy
MichelleLacy

asked on

Retrieve combo box selection

I have two forms.  The first form as a combo box with a list of values.  When the user selects one of the items in the combo box, I want to send the results to the second form which contains a sql statement.  The sql statement uses the selectedText from the combox box in its WHERE statement.  I am having problems retrieving the selected text.  see code snippet.  please help....


public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public static object seqType;
 
        private void cmboID_SelectedIndexChanged(object sender, EventArgs e)
        {
            seqType = cmboID.SelectedText.ToString();
        }  
    }
 
-------
 
public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
 
....connection string inforamtion....
 
string sql = "SELECT column1, columns2 FROM table1 WHERE column1 = '" + Form2.seqType + "'";
....etc

Open in new window

Avatar of thiyaguk
thiyaguk
Flag of India image

Try to Give


   in form2   combobox selected index changed Event : instead of  seqType = cmboID.SelectedText.ToString();

      seqType =  ComboBox1.SelectedItem.ToString  
SOLUTION
Avatar of philipjonathan
philipjonathan
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MichelleLacy
MichelleLacy

ASKER

I tried both selectedItem and selectedValue and I get the following error:
System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
 
What am i doing wrong

 
You can get the selected value from a combo box by using the following command:

seqType = cmboID.Items[cmboID.SelectedIndex].ToString();
when do you create the 2nd form?.. is it when the user selects in the comboBox? or something else?
if you create the 2nd form when the user selects in the comboBox, you can pass the value selected to the contructor

something like this:
public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public static object seqType;
 
        private void cmboID_SelectedIndexChanged(object sender, EventArgs e)
        {
            seqType = cmboID.SelectedText.ToString();
        }  
    }
 
-------
 
public partial class Form3 : Form
    {
        string selected;
        public Form3(string itemSelected)
        {
            InitializeComponent();
 
            selected = itemSelected;
        }
 
 
string sql = "SELECT column1, columns2 FROM table1 WHERE column1 = '" + selected  + "'";

Open in new window

oopss sorry.. i forgot to change the code in Form2
public partial class Form2 : Form
    {
        Form3 frm;
        public Form2()
        {
            InitializeComponent();
        }
        public static object seqType;
 
        private void cmboID_SelectedIndexChanged(object sender, EventArgs e)
        {
            frm = new Form3(cmboID.SelectedText.ToString())
            frm.ShowDialog();
        }  
    }

Open in new window

or you could create a public static string variable in Form3 so that you can update it..
public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
 
        private void cmboID_SelectedIndexChanged(object sender, EventArgs e)
        {
            Form3.SelectedItem = cmboID.SelectedText.ToString();
        }  
    }
 
 
public partial class Form3 : Form
    {
        public static string SelectedItem;
        public Form3()
        {
            InitializeComponent();
            string sql = "SELECT column1, columns2 FROM table1 WHERE column1 = '" + selected  + "'";
            // use sql in the code
        }
    }
 
 

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So I tried "seqType = cmboID.Items[cmboID.SelectedIndex].ToString();"  It did not work, but it gave me an error that provided additional information that might help you help me.  The error is :  System.ArgumentOutOfRangeException was unhandled
  Message="InvalidArgument=Value of '-1' is not valid for 'index'.\r\nParameter name: index"
 So to answer the question: "when do you create the 2nd form?.. is it when the user selects in the comboBox? or something else?"  Let me give some background on how it is loaded.
There is one combox (combx1) that is populated before cmboID.  cmboID is populated based on what the user selected in combx1.  see code snippet.
 I have a method called BindIdentifierComboBox() that binds these results to cmboID.  see the code in the second section of the code snippet.
Finally I have the sql statement and method at the beginning of the thread.

//to populate cmboID
string sql select column1a, columns2a from table2b WHERE column1a = '" + combx1.SelectedValue + "' 
 
BindIdentifierComboBox()
  DataTable id = new DataTable();
                    idAdapter.Fill(id);
                    cmboID.DataSource = id;
                    cmboID.DisplayMember = "source";
                    cmboID.ValueMember = "source";
                    cmboID.SelectedIndex = -1;
                    cmboID.SelectedIndexChanged +=new System.EventHandler(this.cmboID_SelectedIndexChanged);

Open in new window

So solution: ID:22160798; Author:margajet24; Date:08.05.2008 at 08:47AM CDT, did not produce any errors which is great.  But I but in a break point to see if SelectedItem had any items, and it does not.  It appears it is not picking up the user selection...
please try this instead..
public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
 
        private void cmboID_SelectedIndexChanged(object sender, EventArgs e)
        {
            Form3.SelectedItem = cmboID.Items[cmboID.SelectedIndex].ToString();
        }  
    }
 
 
public partial class Form3 : Form
    {
        public static string SelectedItem;
        public Form3()
        {
            InitializeComponent();
            string sql = "SELECT column1, columns2 FROM table1 WHERE column1 = '" + SelectedItem + "'";
            // use sql in the code
        }
    }

Open in new window

A combination of the two selected solutions did the trick!!!  Thanks