我想用列表框和数据库表中的数据填充文本框。文本框正在从数据库中获取数据,但它只选择表的最后一行并且在列表框选择上没有改变。
我的代码:
private void agentList_SelectedIndexChanged(object sender, EventArgs e)
{
string query = "Select * From tblEmployeeDetails";
SqlConnection cn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(query, cn);
SqlDataReader reader;
{
try
{
cn.Open();
reader = cmd.ExecuteReader();
{
while (reader.Read())
{
AgentId.Clear();
AgentId.Text = reader.GetInt32(0).ToString();
FullNameTxb.Text = reader.GetString(2);
emailtxb.Text = reader.GetString(8);
winLogtxb.Text = reader.GetString(1);
passwordtxb.Text = reader.GetString(3);
dobtxb.Text = reader.GetString(6);
addresstxa.Text = reader.GetString(9);
contactNotxb.Text = reader.GetString(10);
niNotxb.Text = reader.GetString(11);
contactNotxb.Text = reader.GetString(10);
sortCodetxb.Text = reader.GetString(12);
accountNotxb.Text = reader.GetString(13);
startdatetxb.Text = reader.GetString(14);
finsishdatetxb.Text = reader.GetString(15);
teamcmb.Text = reader.GetString(16);
basicsRatetxb.Text = reader.GetDecimal(17).ToString();
commRatetxb.Text = reader.GetDecimal(18).ToString();
contracthourstxb.Text = reader.GetInt32(19).ToString();
mainShifttxb.Text = reader.GetString(20);
fridayShifttxb.Text = reader.GetString(4);
activecmb.Text = reader.GetBoolean(5).ToString();
nokNametxb.Text = reader.GetString(21);
nokNumtxb.Text = reader.GetString(21);
gendercmb.Text = reader.GetString(23);
empTypesmb.Text = reader.GetString(24);
securitycmb.Text = reader.GetString(27);
//mondayckb.Checked = reader.GetBoolean(26);
//tuedayckb.Checked = reader.GetBoolean(27);
//wendnesdayckb.Checked = reader.GetBoolean(28);
//thursdayckb.Checked = reader.GetBoolean(29);
//fridaydkb.Checked = reader.GetBoolean(30);
//saturdayckb.Checked = reader.GetBoolean(31);
//sundaycbk.Checked = reader.GetBoolean(32);
//mondayckb.Checked = reader.GetBoolean(25);
//tuedayckb.Checked = reader.GetBoolean(25);
//basicsRatetxb.Text = reader.GetString(16);
//activecmb.Text = reader.GetBoolean(15).ToString();
//dateTimePicker1.Value = reader.GetDateTime(5);
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
cn.Close();
}
}
}