private void frmNSS5_Load(object sender, System.EventArgs e)
{
SqlConnection con;
SqlCommand cmd;
SqlDataReader dr;
con = new SqlConnection(@"workstation id = PC-PC; user id=sa;Password=sapassword; data source=pc-pc; persist security info=True; initial catalog=CleanPayrollTest2");
cmd = new SqlCommand("SELECT IsNull(ArEmpName,'') + ' ' + IsNull(ArFatherName,'') + ' ' + IsNull(ArLastName,'') as EmpName, ID as ID FROM [Emp] ", con);
try
{
con.Open();
dr = cmd.ExecuteReader();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
this.cbEmpName.ValueMember = "ID".ToString();
this.cbEmpName.DisplayMember = "EmpName";
this.cbEmpName.DataSource = ds.Tables["EmpName"];
while (dr.Read())
{
if(dr[0].ToString().Length > 0)
{
this.cbEmpName.Items.Add(dr[0].ToString());
}
}
con.Close();
}
catch
{
MessageBox.Show("Connection Failed");
}
}
private void comboEmpName_SelectedIndexChanged(object sender, System.EventArgs e)
{
MessageBox.Show("Emp ID:" + ' ' + this.cbEmpName.SelectedValue + ", " + "EmpName:" + ' ' + this.cbEmpName.SelectedItem );
}
我在选择员工时没有得到 ID,消息框只显示我的名字......谁能告诉我我的错在哪里?非常感谢