0

I am using c# and winforms 4.0 to create a lo gin form using entity framework. Question is is my logic right. I am just a we bit worried about the return if it doesn't find a record i add a blank return record is that the correct way using entity framework to return a empty record as I want to pass the information found on the log in screen to the main form.

public NaviHrUsers  ValidateUser(string username,string password)
         {

             try{
                 NaviHrUsers currentUser = naviEntities.NaviHrUsers.FirstOrDefault(r => r.login == username);

                 if (currentUser != null)
                 {
                     return currentUser;
                 }

                 else
                 {
                     NaviHrUsers nu = new NaviHrUsers();
                     nu.login = "";
                     nu.password = "";
                     nu.last_login = Convert.ToDateTime("1900/01/01");
                     nu.last_time = "";
                     nu.password_last_changed = Convert.ToDateTime("1900/01/01");
                     return nu;
                 }
             }
             catch (Exception ex)
            {
                throw new EntityContextException("ValidateUser failed.", ex);
            }
         }

1 Answer 1

1

You probably don't want to return a blank record. Instead, you want to return null. Also, if you look at Microsoft's Forms Auth in Asp.Net, Membership.ValidateUser() method returns a bool, which all it does is validate the user, nothing more. For this scenario, I wouldn't consider returning an empty record a good practice.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.