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; …