Click here to Skip to main content
11,492,279 members (63,384 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#
Good Day

i am trying to save text entered in a combo box into the database. however i have a concatenated field in the combo box and i don t want to save the entire text into the database.
below is my code.

private void frmEmployeesJobs_Load(object sender, EventArgs e)
            string empSql = string.Format("Select  (CAST(employee_id AS VARCHAR(MAX))+ ' - '+employee_name) as employee FROM employee");
            DataTable empTable = FillTable(empSql);
            cmbEmp.DisplayMember = "employee";
            cmbEmp.ValueMember = "employee";
            cmbEmp.DataSource = empTable;
            string jobSql = string.Format("Select  (CAST(job_id AS VARCHAR(MAX))+ ' - '+job_name) as job, job_price  FROM job");
            DataTable jobTable = FillTable(jobSql);
            cmbJob.DisplayMember = "job";
            cmbJob.ValueMember = "job_price";
            cmbJob.DataSource = jobTable;
        private void cmbJob_SelectedIndexChanged(object sender, EventArgs e)
            if (cmbJob.SelectedIndex == -1)
                txtPrice.Text = string.Empty;
                txtPrice.Text = cmbJob.SelectedValue.ToString();
Posted 12-Feb-13 6:19am
CHill60 at 12-Feb-13 11:28am
What *do* you want to save in the database from the selected text?
richcb at 12-Feb-13 11:29am
What is the problem? Any errors? Have you tried debugging?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can split the selected text via the separater (if you have one) and then store only that portion that you want to store.
SQL Server 2008 - Loop through/split a delimited string[^] is a simple tip that allows you to split a string based on a delimiter.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

If you want to save only text not Id from drop down then split text of drop down like

string str=cmbJob.SelectedValue.ToString();
string[] arr= str.Split('-');
//in arr[0] job_Id will be there.
And in arr[1] job_name will be there.
you can save what u want.
Georges23 at 12-Feb-13 15:19pm
hi shobhana i tried what you suggested but i am facing this problem.
the problem is i am setting the ValueMember of the combobox to a different field so when i for example say ' string str = cmbJob.SelectedValue.ToString();' it retrives that particular field and not the concatenated string in the combo box.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

try this...
txtPrice.Text = cmbJob.SelectedValue.Substring(cmbJob.SelectedValue.ToString().IndexOf("_")+1);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 404
1 Maciej Los 266
2 KarstenK 220
3 Sascha Lefèvre 215
4 CPallini 208
0 Sergey Alexandrovich Kryukov 9,943
1 OriginalGriff 8,610
2 Sascha Lefèvre 3,609
3 Maciej Los 3,372
4 Richard Deeming 2,440

Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100