C# How to pick Person id from listbox string?

I have listbox which has many rows.
First word in every row is PersonId.
I want pick this Person Id and use it in SQL Query like this: " SELECT * FROM mydatatable WHERE ID = '"+ PersonId+"'.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I think you need to look into SelectedItem

Here are the properties

miksuFinAuthor Commented:
This is desktop -application, not web-app.
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

miksuFinAuthor Commented:
I suppose that the code is something like this

int item = listBox1.ValueMember.First;
SelectedValue didn't work?
miksuFinAuthor Commented:
OR something like that
 int item = Convert.ToInt32(listBox1.ValueMember.First(listBox1.Items));
miksuFinAuthor Commented:
I tried this;  int item = Convert.ToInt32(listBox1.SelectedValue);
, but it didn't work.
miksuFinAuthor Commented:
this works almost:
   int item = Convert.ToInt32(listBox1.SelectedItem.ToString());

It shows wanted row like this : {SelectedItem= "3\t\tSometext\t\tSometext\t\tSometext"}

Problem is how to pick "3" from the string.
miksuFinAuthor Commented:
I tried also this:

  string teksti = listBox1.SelectedItem.ToString();

    int item = Convert.ToInt32(teksti.Substring(0, teksti.IndexOf(" "))); <-- Converting problem!
miksuFinAuthor Commented:
This works:

string teksti = listBox1.SelectedItem.ToString();

    int item = Convert.ToInt32(teksti.Substring(0, teksti.IndexOf("\t\t ")));
good that you figured  it out.
Chris StanyonWebDevCommented:
An alternative approach is to add objects to your ListBox instead of strings. The SelectedItem property of the ListBox will then contain an object and you can simply get a property:

yourClass item = (yourClass)listbox1.SelectedItem;
int personID = item.PersonId;

Open in new window

You could also override the ToString() method in your class to return the PersonId:

var personId = listbox1.SelectedItem.ToString();

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.