Hi

I have a stored procedure that returns the unique ID for a particular Name/string passed in.

On my windows form I have a ListBox control filled with a list of names (returned from another stored procedure)

I want to be able to let the user click the list box and select a name ... then once they have selected the name that value would be passed into my new stored procedure to return the unique ID which i then want to display in a hidden textbox for further usage.

Can anyone point me in the right direction with this.

The following uncommented lines of code illustrate what I am trying to do to get the contents of the users selection

//myCommand.Parameters["@companyName"].Value = cbo_companyName.SelectedText;
//myCommand.Parameters["@companyName"].Value = cbo_companyName.SelectedItem;

Many Thanks :)

Elmo

If your listbox holds the string of the name then you would need a dictionary to pull out the relevant item against that name, or, if you create an object whos "tostring" returns the name you could just add the whole object to the listbox, knowing it will display the right thing, but then its the selected item, with no looking up required.

Holding things in an invisible text box would seem an odd way to do it, surely you just want a private or public variable for the form class?

oooh yea lol

but still ... surely there is a simple property of the comboBox control to force the DATA to be passed to a VARIABLE (which could be used for further manipulation)

I have used a dropdownlist control in ASP.NET before where I was able to say

myCommand.Parameters["@companyName"].Value = ddl_companyName.SelectedValue;

and that value was passed into the stored procedure and executed correctly.

I dont know what you mean by dictionary ... Ive tried googling but im still none the wiser.

I must be being thick.

You get the item selected from a list box with

listBox1.SelectedItem

A dictionary is exactly that, it allows you to lookups on things.

Ooops I mean a ComboBox

I must be being thick.
listBox1.SelectedItem

Yes, I have tried that. But for some reason its not working. I am probably doing something in the wrong order.

private void cbo_companyName_SelectedIndexChanged(object sender, EventArgs e)
        {
            getCompanyName(); //populates the Cbox with the data - it works
            getCompanyID();  //returns the uniqueID for the selected item - doesnt work
        }

Above shows the method calls I have in the SelectedIndexChanged event

public void getCompanyName() //Method to return list of company names and add to listbox
        {
            //Clear the listbox
            cbo_companyName.Items.Clear();
            
            //Create SQL command, declare SPROC and connection to database
            myCommand = new SqlCommand("get_CompanyName", myConnection);
            myCommand.CommandType = CommandType.StoredProcedure;
            
            //Open database connection
            myConnection.Open();

            //Execute myCommand into the reader and close the connection
            myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            
            while (myReader.Read())//== true)
            {
                //Loop through the reader appending each element to the list                
                cbo_companyName.Items.Add(myReader.GetString(1));
            }
            //Close and dispose of open properties
            myReader.Close();
            myCommand.Dispose();
        }

Now the following method is the one that returns the Unique ID, I tested it using two text boxes, one where I ran the program and manually entered a Name, then clicked a button and the ID was displayed in the second textbox.

So, I just need to force it to take the selected value and pass it into the stored procedure

public void getCompanyID()
        {
            //Create SQL command, declare SPROC and connection to database
            myCommand = new SqlCommand("findCompanyid", myConnection);
            myCommand.CommandType = CommandType.StoredProcedure;

            //Open database connection
            myConnection.Open();

            myCommand.Parameters.Add(new SqlParameter("@companyName", SqlDbType.VarChar, 50, "CompanyName"));
            //myCommand.Parameters["@companyName"].Value = cbo_companyName.SelectedText;
            //myCommand.Parameters["@companyName"].Value = cbo_companyName.SelectedItem;
            myCommand.Parameters["@companyName"].Value = cbo_companyName.SelectedValue;
            //myCommand.Parameters["@companyName"].Value = listBox_CompanyName.SelectedValue;

            myReader = myCommand.ExecuteReader();

            while (myReader.Read())
            {
                int temp = (myReader.GetInt32(0));
                txt_hidden_ID_Company.Text = temp.ToString();
            }
            //Close and dispose of open properties
            myReader.Close();
            myCommand.Dispose();
            myConnection.Close();
        }
getCompanyName(); //populates the Cbox with the data - it works
            getCompanyID();  //returns the uniqueID for the selected item - doesnt work

I wouldnt expect getCompanyID() to work, you havent selected an item after clearing the box and repopulating it.

yeah fair point !

In ASP.NET there is an onPageLoad() (or something similar) so that you can force a DropDownList to be automatically filled with data ... the user then clicks their desired info and then the SelectedIndexChanged event is fired ... I guess thats why it worked for me before then.

So with Windows Forms - how then do I make sure the Combox box is already filled before the user clicks it (ie .. "Select..." is displayed then the user clicks the dropdown arrow to see the available data).

Ive been playing about with it but havent been having much luck with it

double click on the form, it will give you the forms onload procedure, you can fill it in there.

I got the combo box issue sorted

was able to get the SelectedIndex value and from there I get the SelectedItem (string)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.