Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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.

C#
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;
            }
            else
            {
                txtPrice.Text = cmbJob.SelectedValue.ToString();
            }
        }

Regards
Posted
Comments
CHill60 12-Feb-13 11:28am    
What *do* you want to save in the database from the selected text?
Richard C Bishop 12-Feb-13 11:29am    
What is the problem? Any errors? Have you tried debugging?

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.
 
Share this answer
 
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.
 
Share this answer
 
Comments
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.
try this...
SQL
txtPrice.Text = cmbJob.SelectedValue.Substring(cmbJob.SelectedValue.ToString().IndexOf("_")+1);
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900