0

I am trying to display item values in their respective textbox when an item is selected in the listbox

this is how I have it set up:

  1. This is how the values are shown to the list box

            command.CommandText = "select * from ItemsList";
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                EditItemBrowserBox.Items.Add(reader["ID"].ToString() + "         " + reader["ItemBrand"].ToString() + "  " + reader["ItemName"].ToString() + "  " + reader["ItemType"].ToString() + "  " + reader["ItemPrice"].ToString());   
            }
    
  2. This is how i'm trying to make the values from the listbox show to a textbox

            command.CommandText = "select * from ItemsList where ItemName='" + EditItemBrowserBox.Text + "' ";
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                EditIDTB.Text = reader["ID"].ToString();
                ItemNameAddTB.Text = reader["ItemName"].ToString();
                ItemTypeAddTB.Text = reader["ItemType"].ToString();
                ItemBrandAddTB.Text = reader["ItemBrand"].ToString();
                ItemPriceAddTB.Text = reader["ItemPrice"].ToString();
            }
    

EditItemBrowserBox is the listbox and EditIDTB.Text = reader["ID"].ToString();is me trying to show the value in one of the textboxes.

Right now, when I click on an item nothing shows in the text boxes. Any help appreciated

1
  • so how would I be able to make it look for the value in the string? Commented Feb 7, 2019 at 15:57

1 Answer 1

1

There are better ways to accomplish what you're doing, so you can take advantage of DisplayMember and ValueMember, but the problem you're having stems from using the entire Text field that you're displaying to the user.

If their selection looked like this:

001     Acme  Anvil  Weapon  10.00

Then your query ends up being:

select * from ItemsList where ItemName='001     Acme  Anvil  Weapon  10.00'

Just split the part off you're interested in:

var itemName =
    EditItemBrowserBox.Text.Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries)[2];

command.CommandText = $"select * from ItemsList where ItemName='{itemName}'";

To get around issues with item names that have multiple spaces, you might want to select based on the ID instead:

var id = EditItemBrowserBox.Text.Split(' ')[0];

command.CommandText = $"select * from ItemsList where ID='{id}'";

You might also want to check into parameterizing your queries, but that's a different issue...

Sign up to request clarification or add additional context in comments.

3 Comments

it works thank you very much, but it only works when the ItemName is a single word but it doesn't work when there are 2 words in the item name, I made sure to split when there are 2 spaces only
i'll look into it, thank you, I wish I could upvote you
just so that people can see: To be able to use spaces, you can sort by ID instead of itemname

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.