Save value from the combobox for the index that will be changed.

For e.g: If I have 5 items listed and 1st one is selected and when I change index to suppose 4th one, I need to save the value of 1st listed item at time of index being changed and next time if I select 2nd item on the list it needs to save value for 4th one at time of index change.

I need to pass that value for batch name in the below listed stored procedure every time index is changed so that previous selected index can be unlocked.

cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "usp_CAMR_UnlockedBatchName";
                    cmd.Parameters.AddWithValue("@Batch_Name", temp);
                    cmd.Parameters.AddWithValue("@lst_mod_userid", Environment.UserName);

Dani AI

Generated

A simple, reliable pattern is to keep the previous selection in a field and use that stored value when the user changes the combo box. Relying on SelectedText is fragile (it refers to a text selection inside an editable box and is often empty for DropDownList), so persist the identity you care about (index, key or displayed string) yourself and update it only after you use it to unlock the previous batch.

Example pattern (WinForms/C#):

private string _previousBatch; // or object/_previousKey if data-bound

private void Form_Load(object sender, EventArgs e)
{
    _previousBatch = comboBox1.SelectedItem?.ToString();
}

private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
    var newBatch = comboBox1.SelectedItem?.ToString();

    if (!string.IsNullOrEmpty(_previousBatch))
    {
        // call DB to "unlock" _previousBatch (do this off the UI thread)
        _ = UnlockPreviousAsync(_previousBatch);
    }

    _previousBatch = newBatch;
}

private async Task UnlockPreviousAsync(string batchName)
{
    // perform parameterized stored-proc / SQL call here on a background thread.
    await Task.Run(() => { /* ADO.NET call: use parameters, don't concatenate SQL */ });
}

Notes and troubleshooting:

  • Use SelectionChangeCommitted to capture user-initiated changes; SelectedIndexChanged also fires for programmatic changes.
  • If the ComboBox is data-bound, prefer SelectedValue (or cast SelectedItem to your item type) rather than ToString().
  • Initialize _previousBatch safely (null checks). For responsive UI, run DB calls asynchronously and consider debouncing or cancelling if selections change rapidly.

Tie-in to thread: observed SelectedText being empty — that behavior is expected for non-editable combo boxes. 's suggestion to look at selection events and 's idea to track the index are both valid; the stored-previous-value + SelectionChangeCommitted approach combines those ideas and avoids relying on transient SelectedText.

Recommended Answers

All 3 Replies

Have a look at the selectedvaluechanged event.

SelectedIndexChanged event of combobox is unable to help you as if you try to get the SelectedText value in a SelectedIndexChanged or SelectedValueChanged event handler, the property returns an empty string. This is because, at the time of these events, the previous SelectedText value has been cleared and the new value has not yet been set.

Keep track of "SelectedIndex" property in "SelectedIndexChanged" event handler.

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.