Solved

Combo box not showing database value

Posted on 2011-03-07
6
244 Views
Last Modified: 2012-05-11
I created a combo box that will display an employee name but store the Employee NO.  The code will create the drop down properly, and when I step thru the code, it is obtaining the correct EmployeeNO to update, but it only displays the first employee name in the list.  I'm sure this is a simple problem, but I don't see what I have done wrong.

on Load:
LoadEmployees();
 LoadPatient();

  private void LoadEmployees()
        {
            m_EmployeeNames = new EmployeeSUID();
            try
            {
                m_EmployeeNames.GetEmployees();
                m_dtEmployee = m_EmployeeNames.GetEmployee;
                // Setup the combobox view and display-, value member
                cbxPreferredCaller.DataSource = m_dtEmployee;
                cbxPreferredCaller.DisplayMember = "FullName";
                cbxPreferredCaller.ValueMember = "EmployeeNO";
                cbxPreferredCaller.DataBindings.Add("SelectedValue", m_dtEmployee, "EmployeeNO");
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                   
            }

 private void LoadPatient()
        {
            int index=0;

            try
            {
                m_patientData = m_mdiParentForm.PatientSUID;
     
                //Set PrimaryCaller comboBox
                m_EmployeeNames.GetEmployeebyEmplNo(m_patientData.PatientRcd.PrimaryCaller);
               
                cbxPreferredCaller.GetItemText(m_patientData.PatientRcd.PrimaryCaller);
               

                tbxMRN.Text = m_patientData.PatientRcd.MRN.ToString();
             
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

I hope this is enough to see what I have done wrong.
Thanks
0
Comment
Question by:LenTompkins
6 Comments
 
LVL 9

Expert Comment

by:gery128
ID: 35065988
Replace this line :
cbxPreferredCaller.DataBindings.Add("SelectedValue", m_dtEmployee, "EmployeeNO");

with this:
cbxPreferredCaller.DataBind();

It should work.
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 35066198
Hi, For getting value from combobox you have to first bind their values as a ValueMember and DataMember.
0
 
LVL 1

Expert Comment

by:lalitgada
ID: 35067624
check only the datasource and datamember
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:LenTompkins
ID: 35068533
qery128, DataBind is used for web applications and this is a windows application.  
Samirbhoqayta, how do you bind their values as a value member and datamember?  Can you give an example.
0
 

Accepted Solution

by:
LenTompkins earned 0 total points
ID: 35072369
I ended up solving the problem .  Here is what I had to do for a windows application:

 private DataTable m_dtEmployee;
 private BindingSource m_bndEmployee;


OnLoad
 LoadEmployees();
  LoadPatient();

 private void LoadEmployees()
        {
            m_EmployeeNames = new EmployeeSUID();
            this.m_bndEmployee = new BindingSource();
           
            try
            {
                // obtain all employees and store in data table
                m_EmployeeNames.GetEmployees();
                m_dtEmployee = m_EmployeeNames.GetEmployee;
                // set binding source data source to data table
                this.m_bndEmployee.DataSource = m_dtEmployee;
                // set binding source to the data source of combo box
                cbxPreferredCaller.DataSource = m_bndEmployee;
                // Setup the combobox view and display-, value member
                cbxPreferredCaller.DisplayMember = "FullName";
                cbxPreferredCaller.ValueMember = "EmployeeNO";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                   
            }

 LoadPatient()
{
              //Set PrimaryCaller comboBox
                m_EmployeeNames.GetEmployeebyEmplNo(m_patientData.PatientRcd.PrimaryCaller);
                cbxPreferredCaller.Text = m_EmployeeNames.EmployeeName;
}
0
 

Author Closing Comment

by:LenTompkins
ID: 35120677
I finally figured out how to solve the problem
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question