如何从SqlServer获取值并将其填充到其corrspond ComboBox中 [英] How to get value from SqlServer and populate it into its corrspond ComboBox
问题描述
在我的数据库中我有一个表'EmpRole',其中存储员工的各种角色,如CEO,开发人员等。我在我的项目注册表中使用此角色。这些角色显示在组合框中。在注册时,这工作正常,但是当我要在表单上查看注册用户的配置文件时,不会选择相应的组合框值,而是默认选择组合框的第一个值。
我希望在配置文件预览时在组合框中选择适当的emprole值
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
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");
}
}
并且有bindRole ()方法
and there is the bindRole() method
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"];
}
推荐答案
你在保存吗?数据库中的RoleName或RoleID?
当你将值绑定到组合时,你正在使用EmpRole。它是RoleID或RoleName。请检查。保存RoleID或ComboBox.SelectedValue并获取值并使用相同的逻辑绑定到组合。
请检查然后确认。如果有任何混淆,那么你可以问我。
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.
这篇关于如何从SqlServer获取值并将其填充到其corrspond ComboBox中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!