Click here to Skip to main content
13,512,023 members
Rate this:
Please Sign up or sign in to vote.
See more:
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 5:19am
CHill60 12-Feb-13 11:28am
What *do* you want to save in the database from the selected text?
richcb 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 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
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180417.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2018
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