Click here to Skip to main content
15,881,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
in my DB i have a table 'EmpRole' where various roles of an employee are stored like CEO, Developer etc. And i am using this roles in my project registration form. these roles are displaying in a combobox. At time of registration this works fine, but when i m going to view the profile of registered users on the form the appropriate combobox value not get selected instead first value of the combobox selected by default.
I want that appropriate value of emprole get selected in combobox at time of profile preview


C#
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCN"].ConnectionString))
            {
                btnRegister.Text = "Update";

                SqlCommand cmd = new SqlCommand("Usp_Register", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Mode", "ViewProfile");
                con.Open();

                SqlDataAdapter da = new SqlDataAdapter();

                DataTable dt = new DataTable();
                da.SelectCommand = cmd;
                da.Fill(dt);
                if (dt != null && dt.Rows.Count > 0)
                {


                    txtName.Text = dt.Rows[0]["EmpName"].ToString();
                    txtPassword.Text = dt.Rows[0]["Password"].ToString();
                    //txtPassword.Enabled = false;
                    txtUserName.Text = dt.Rows[0]["UserName"].ToString();

                    txtDesignation.Text = dt.Rows[0]["EmpDesignation"].ToString();
                    txtMobileNumber.Text = dt.Rows[0]["Empnumber"].ToString();
                    txtEmailId.Text = dt.Rows[0]["EmpEmail"].ToString();
                    bindRole();
                    comboboxRole.SelectedValue = dt.Rows[0]["EmpRole"].ToString();
                    dateTimePickerJoinDate.Value = Convert.ToDateTime(dt.Rows[0]["JoinDate"].ToString());
                    dateTimePickerDOB.Value = Convert.ToDateTime(dt.Rows[0]["DOB"].ToString());
                    if (dt.Rows[0]["Gender"].ToString() == "Male")
                    {
                        radioButtonMale.Checked = true;

                    }
                    else if (dt.Rows[0]["Gender"].ToString() == "Female")
                    {
                        radioButtonFemale.Checked = true;
                    }
                }

                else
                {
                    MessageBox.Show("No Data in dt");
                }

            }



and there is the bindRole() method


C#
public void bindRole()
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCN"].ConnectionString))
            {

                DataSet ds = new DataSet();

                SqlDataAdapter da = new SqlDataAdapter("Select * from EmpRole Order by RoleName", con);
                con.Open();
                da.Fill(ds, "EmpRole");
                comboboxRole.DisplayMember = "RoleName";
                comboboxRole.ValueMember = "RoleId";
                comboboxRole.DataSource = ds.Tables["EmpRole"];
            }
Posted

1 solution

Hi,

Are you saving RoleName or RoleID in database?

When you are binding value to combo then you are using "EmpRole". It is RoleID or RoleName. Please check. Save RoleID or ComboBox.SelectedValue and fetch value and bind to combo using same logic.

Please check then confirm. If there is any confusion then you can ask me.
 
Share this answer
 
Comments
Member 10276989 12-Jul-14 4:03am    
I am saving RoleId in the DataBase. but at run time it displaying the value for role name correspond to its RoleId

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