Get "EMP_ID" or Primary Key from a ComboBox selection , SQL

Posted 17 June 2012 - 02:53 PM

Hello,

I've been practicing with C# SQL coding and pulling/inserting data into a database. Now, I'm to the point where I want to edit what's in my tables.

My simple solution was (for testing purposes) was to have a combobox populate with EMP_LNAME, EMP_FNAME (successfully working) and when selected to fill in a series of textboxes with the rest of the data in that row.

My problem is actually getting the EMP_ID from that combobox selection so I can correct use my WHERE EMP_ID = "Combobox Value".

Run the project and select different items. You'll see the label change as you select them. Now this isn't the best code (no error checking, should use sender in the event rather than the hard coded combobox1, etc.) but it gives you enough to create what you need for your project.

Re: Get "EMP_ID" or Primary Key from a ComboBox selection , SQL

Posted 18 June 2012 - 05:58 AM

Also, you should read this post on parameterizing your queries (instead of concatenating variables into your queries). Reasons you should do this are listed in the post, so I won't list any here, but suffice it to say that this is a better, more secure way of interacting with the DB.

Re: Get "EMP_ID" or Primary Key from a ComboBox selection , SQL

Posted 18 June 2012 - 05:24 PM

Momerath,

I'm not really following where this data pulls the EMP_ID columns information?

h4nnib4l, on 18 June 2012 - 06:58 AM, said:

Also, you should read this post on parameterizing your queries (instead of concatenating variables into your queries). Reasons you should do this are listed in the post, so I won't list any here, but suffice it to say that this is a better, more secure way of interacting with the DB.

Yes, I've already done that for my insert queries. Just haven't moved over to my SELECT queries yet. Thanks for the link, very helpful.

So, I select a employee from the combobox, I'm not following how to "get" the data from what is virtually in that listing. Do I just pull the data and place into variables, then populate the combo box from there?

Sorry for so much trouble, I've only had college courses on C# and SQL within C# is totally new (as you can see!).

You are filling the combo box with Data objects. Data objects will have the name and id. You will have set the property mentioned above to "Name", then you'll get back a Data object from SelectedItem. Use the ID property from the Data object and you'll have the EMP_ID of the name that was selected.

Re: Get "EMP_ID" or Primary Key from a ComboBox selection , SQL

First, if you don't define a ToString for that class, you aren't going to get a very helpful listing. Second, never add values one at a time. Even in a simple example, it teaches bad habits.

Rather:

class Data {
public String Name { get; private set; }
public int ID { get; private set; }
public Data(String name, int id) {
this.Name = name;
this.ID = id;
}
// this is important, this it what will show in the combo box
public override string ToString() { return this.Name; }
}
public Form1() {
InitializeComponent();
this.comboBox1.DataSource = new Data[] {
new Data("one", 1), new Data("two", 2), new Data("three", 3)
};
// note, this will often be the result of a data pull
// usually a List of object or a DataTable
}