如何在对象列表中搜索? [英] How to search in object list?
本文介绍了如何在对象列表中搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在对象列表中搜索时遇到麻烦,我想比较这些值并返回该人的信息,但是问题是,对于未找到的每条记录,它都会给我一个错误.
Member_Login objM = 新 Member_Login();
列表< member_login> objList = 新 List< member_login>();
objList = objM.Get_Member_Details();
foreach (Member_Login M in objList中的成员)
{
如果(txtUsername.Text == M.Username&& txtPassword.Text == M.Password)
{
lblresult.Text = M.Member_Id.ToString();
会话[" ] = M.Member_Id.ToString();
Response.Redirect(" );
}
其他
{
MessageBox.Show(" , 错误!无效的帐户",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
} </ member_login >
解决方案
我认为您需要清理并重新命名变量.您似乎将Member_Login
用作类,将member_login(small initials)
用作该类的属性.但是,然后在您的foreach循环中将Member_Login (Capitals)
用作属性.就个人而言,这就是我认为您出了点问题的地方.请参见密码存储:操作方法. [ ^ ]
您的代码逻辑错误:错误条件应仅在循环结束时发出信号.
我不是ASP专家,但以下代码可以解决问题:
foreach (Member_Login M in objList) { if (txtUsername.Text == M.Username && txtPassword.Text == M.Password) { lblresult.Text = M.Member_Id.ToString(); Session["Memid"] = M.Member_Id.ToString(); Response.Redirect("Home.aspx"); } } // ''redirect'' didn''t occur: member not found MessageBox.Show("You are not found as a registered member", "Error!! Invalid Account", MessageBoxButtons.OK, MessageBoxIcon.Warning);
I''m having trouble searching in an object list, I want to compare the values and return that persons info, but the problem is that it gives me an error for each record not found.
Member_Login objM = new Member_Login();
List<member_login> objList = new List<member_login>();
objList = objM.Get_Member_Details();
foreach (Member_Login M in objList)
{
if (txtUsername.Text == M.Username && txtPassword.Text == M.Password)
{
lblresult.Text = M.Member_Id.ToString();
Session["Memid"] = M.Member_Id.ToString();
Response.Redirect("Home.aspx");
}
else
{
MessageBox.Show("You are not found as a registered member", "Error!! Invalid Account", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}</member_login>
解决方案
I think you need to clean up and do some renaming of variables. You seem to be usingMember_Login
as a class andmember_login(small initials)
as a property of the class. But then you useMember_Login (Capitals)
as the property in your foreach loop. Personally, that is where I think you are going wrong somehow.
I agree with Wayne, but would add that you should never store passwords in clear anyway. See Password Storage: How to do it.[^]
The logic of your code is wrong: the error condition should be signaled only at the end of the loop.
I''m not a ASP expert but the following code should do the trick:
foreach (Member_Login M in objList) { if (txtUsername.Text == M.Username && txtPassword.Text == M.Password) { lblresult.Text = M.Member_Id.ToString(); Session["Memid"] = M.Member_Id.ToString(); Response.Redirect("Home.aspx"); } } // ''redirect'' didn''t occur: member not found MessageBox.Show("You are not found as a registered member", "Error!! Invalid Account", MessageBoxButtons.OK, MessageBoxIcon.Warning);
这篇关于如何在对象列表中搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文