我有一个列表并已为其添加值,但是它仅显示从中提取值后添加的最后一个值。为什么?
DataTable dt = ClsContractor.GetContractorsPasswordsWhereEqualsCNIC();
Contractors c = new Contractors();
DataRow dr = dt.Rows[0];
int i = 0;
foreach (DataRow r in dt.Rows)
{
string password = Utility.Decrypt(Convert.ToString(r["Password"])).Trim();
string CNIC = Convert.ToString(r["CNIC"]).Trim();
c.NewPwd = Convert.ToString(r["NewPwd"]).Trim();
if (password == CNIC)
{
c.ContractorID = Convert.ToInt32(r["ContractorID"]).ToInt32();
lstContractorsID.Add(c);
Utility.Msg_Success(Master, "Pwd: " + password + " and CNIC: " + CNIC + " matched :" + i++.ToString());
}
}
答案 0 :(得分:3)
我认为这是因为您覆盖了“承包商c”对象。
Contractors c = new Contractors();
应该在foreach循环中声明。