Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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>
Posted
Updated 12-Sep-11 23:46pm
v3

I think you need to clean up and do some renaming of variables. You seem to be using Member_Login as a class and member_login(small initials) as a property of the class. But then you use Member_Login (Capitals) as the property in your foreach loop. Personally, that is where I think you are going wrong somehow.
 
Share this answer
 
I agree with Wayne, but would add that you should never store passwords in clear anyway. See Password Storage: How to do it.[^]
 
Share this answer
 
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);
 
Share this answer
 
Comments
BobJanova 13-Sep-11 7:41am    
Good but you need a 'return' or a state variable to prevent the message being shown in the case that a redirect does happen.
CPallini 13-Sep-11 7:53am    
Do you mean "if Redirect fails"?
You are basically comparing all the usernames and passwords against data entered by the user.
If the condition (txtUsername.Text == M.Username && txtPassword.Text == M.Password) fails, the code displays the message box.

This is not correct code. Do not display the messagebox every time the username and password do not match.
Instead use a flag to keep track of the match status. If there is a match, set the flag and break out of the loop.

Check for a condition on the flag after the loop. If the flag is not set, display the message.
 
Share this answer
 
Something must have happened when i copied the code coz i only used the Member_Login class. It still gives me the same problem.
Is there another way to structure my foreach so it that it only returns the correct value without returning multiple error messages.

C#
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></member_login>



Edit: Code formatted
 
Share this answer
 
v3
lol you guys are the best... its working now.. Thanx guys.
 
Share this answer
 
Comments
BobJanova 13-Sep-11 7:43am    
Two comments on CP Q&A etiquette/process:
- If you want to comment on your question, you should use 'Have a question or comment' or 'Improve question' (to edit the initial post). Don't use 'Add Solution' to add a comment or update.
- If an answer is helpful and answers your question, you should accept it as an answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900