Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I fetched all the username in Combobox1 displaymember as username and Id as value member.

Now I enabled a groupbox having a textbox,3 combobox and update button when i select an item in Combobox1.considering the valuemember of combobox1 fetching particular user and trying to display those details in groupbox(textbox contains the username,3 combo boxes having department designation role details) .My Database table having foreign keys(deparment,designation and role).It returns only 3 combobox value members not the display memeber(Text).How can i link these tow so that upon selecteing username in Combobox1 the corresponding values should appear in textbox as well as 3 comboboxes.

here is my code:

C#
private void cmbBoxUpdateEmployee_SelectedIndexChanged_1(object sender, EventArgs e)
    {
          changecmbBoxValue();

    }     panelupdateEmployee.Visible = true;


    public void changecmbBoxValue()
    {

        try
        {
            textBoxUpdateEmployee.Enabled = true;
            cmbBoxUpdateEmpRoleID.Enabled = true;
            cmbBoxUpdateEmpDepartment.Enabled = true;
            cmbBoxUpdateEmpDesignation.Enabled = true;
            dbobj.Open();
            DataSet ds = new DataSet();
            if (!string.IsNullOrEmpty(cmbBoxUpdateEmployee.SelectedValue.ToString()))
            {
                string cmd = "select Name,RoleId,DepartmentId,DesignationId from tblEmployee where Name='" + cmbBoxUpdateEmployee.SelectedValue+ "'";

      //I get system.Datarow conversion not done error.
                ds = dbobj.ExecuteDataset(cmd);
                if ((ds.Tables[0].Rows.Count) > 0)
                {
                textBoxUpdateEmployee.Text = Convert.ToString(ds.Tables[0].Rows[0]          [0].ToString());
              cmbBoxUpdateEmpRoleID.SelectedIndex = Convert.ToInt32(ds.Tables[0].Rows[0][1].ToString());
                    cmbBoxUpdateEmpDepartment.SelectedIndex = Convert.ToInt32(ds.Tables[0].Rows[0][2].ToString());
                    cmbBoxUpdateEmpDesignation.SelectedIndex = Convert.ToInt32(ds.Tables[0].Rows[0][3].ToString());
                    lblupdatedelete.Text = " Update Successfully";
                    EmployeeUpdateInfo();
                    textBoxUpdateEmployee.Clear();
                }
            }              

        }
        catch (Exception ex)
        {
            MessageBox.Show("Error!!" + ex.Message);
        }
        finally
        {
            dbobj.Close();
        }


    }

How can i achieve this? any idea.
Posted
Updated 4-Apr-13 8:48am
v2

1 solution

Try to change:
C#
textBoxUpdateEmployee.Text = Convert.ToString(ds.Tables[0].Rows[0]          [0].ToString());
cmbBoxUpdateEmpRoleID.SelectedIndex = Convert.ToInt32(ds.Tables[0].Rows[0][1].ToString());
cmbBoxUpdateEmpDepartment.SelectedIndex = Convert.ToInt32(ds.Tables[0].Rows[0][2].ToString());
cmbBoxUpdateEmpDesignation.SelectedIndex = Convert.ToInt32(ds.Tables[0].Rows[0][3].ToString());

with:
C#
textBoxUpdateEmployee.Text = ds.Tables[0].Rows[0][0].ToString();
cmbBoxUpdateEmpRoleID.SelectedText = cmbBoxUpdateEmpRoleID.FindStringExact(ds.Tables[0].Rows[0][1].ToString());
cmbBoxUpdateEmpDepartment.SelectedText = cmbBoxUpdateEmpDepartment.FindStringExact(ds.Tables[0].Rows[0][2].ToString());
cmbBoxUpdateEmpDesignation.SelectedText = cmbBoxUpdateEmpDesignation.FindStringExact(ds.Tables[0].Rows[0][3].ToString());


Please, follow these links:
combobox.selectedtext[^]
combobox.FindStringExact[^]
combobox.FindString[^]
 
Share this answer
 

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