views:

226

answers:

2

I usually do web programming but have a requirement for a windows form in this project I'm working on now.

I'm using SubSonic to get a list of jobs and load a combobox:

JobCollection jobs = new JobCollection().Load();
cboJobs.DisplayMember = "Name";
cboJobs.ValueMember = "Id";
cboJobs.DataSource = jobs;

When the user selects an item and clicks the search button I need to query the database using the job "Id" field.

TransactionCollection transactions = new TransactionCollection();
if (!String.IsNullOrEmpty(txtTransactionId.Text))
{
    transactions.Where("id", txtTransactionId.Text);
}
else
{
    transactions.Where("raw_data", Comparison.Like, "%" + txtFind.Text + "%");     
}
transactions.Where("job_id", Convert.ToInt32(cboJobs.SelectedItem));
transactions.Load();

The line that is having the problem is the one that goes "Convert.ToInt32(cboJobs.SelectedItem)".

How do I get the job id versus the name?

+6  A: 

You might want to look at the SelectedValue property of your combobox. You set the ValueMember to the ID, so the SelectedValue should contain the correct value.

Drew McGhie
+2  A: 

You have already gotten (a good) answer, but I thought it might be in place to point this out anyway: in your application's combo box, each item will actually be a full Job object, not only a string representation of one. So the SelectedItem property will return the Job object that is currently selected in the ComboBox. You can access any of its members simply by casting it to Job:

Job currentJob = cboJobs.SelectedItem as Job;
if (currentJob != null)
{
    // here you can access currentJob.Name, currentJob.Id or any other member
}
Fredrik Mörk
thanks, that is a good answer. I missed the part about casting it to job. Thanks.
Mike Roosa