0
votes

I have a list box metalListbox and 4 textboxes rtxt_num,name,docname,wiloc.

The listbox is bound to a data source with OleDb, and on

private void metal_metalListbox_SelectedIndexChanged(object sender, EventArgs e)

details from the selected value will show in the textboxes. Everything goes well except when selecting three certain values from the listbox.

  1. 12 Ft Accurpress Press Brake
  2. ETS 2000 (14 Ft Accurpress)
  3. First Article Inspection (Metal)

When I select one of these three, the listbox snaps to the first entry and won't let me select them, but continues working. If I click these again, it will just snap to the first entry again.

I thought it might be the file names or other details in the textboxes that it's showing. Due to the (), but 12 ft accurpress has no (). Maybe due to the numbers, but I have other entries with numbers.

No errors or anything pops up, so it almost works :(

Here's the code:

private void metal_metalListbox_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {         
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        string query = "select * from Metal where Name = '" + metalListbox.Text + "'";
        command.CommandText = query;

        OleDbDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            metal_rtxt_num.Text = reader["Number"].ToString();
            metal_rtxt_name.Text = reader["Name"].ToString();
            metal_rtxt_docname.Text = reader["docName"].ToString();
            metal_rtxt_wiloc.Text = reader["wiLoc"].ToString();
        }             
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error" + ex);
    }
}
2
Try to use the debugger to step though your code. You should see if metalListbox.Text is the problem (it should not!) and how the query works..TaW

2 Answers

0
votes

Your query using the value of metalListBox.Text is most likely to blame here.

Try metalListBox.SelectedItem, or pull that value from the e parameter (which should have a "selectedItem" property).

0
votes

Let try this while(reader.HasRows){} and change the event to selectedItem not selectedIndex Has.Rows