0

I have a login page that Username is the emaloyee ID which is an Integer. however to validate my loging I need to convert it to string, but it continuesly give me same error.

I need help here: var user = db.EMPLOYEEs.Where(u => u.EMP_ID == model.EMP_ID && u.EMP_PASSWORD == model.EMP_PASSWORD).FirstOrDefault();

I have tried all of these but sill give me same error :

string s = i.ToString();
string s = Convert.ToString(i);
string s = string.Empty + i;
string s = new StringBuilder().Append(i).ToString();

enter image description here


I get another error down on

serializeModel.roles = user.RoleROLE_ID; error: Cannot implicitly convert type 'int' to 'string[]'  

what I suppose to do with this one?

enter image description here

here is my code :

 [HttpPost]
        public ActionResult Login(LoginModel model, string returnUrl = "")
        {
            if (ModelState.IsValid)
            {
                int modelID = int.Parse(model.EMP_ID);
                var user = db.EMPLOYEEs.Where(u => u.EMP_ID == modelID && u.EMP_PASSWORD == model.EMP_PASSWORD).FirstOrDefault();
                    if ( user !=null)
                {
                    var roles = db.EMPLOYEEs.Select(m => m.RoleROLE_ID).ToArray();

                    CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
                    serializeModel.UserId = user.EMP_ID;
                    serializeModel.FirstName = user.EMP_FIRST_NAME;
                    serializeModel.LastName = user.EMP_LAST_NAME;
                    serializeModel.roles = user.RoleROLE_ID;
3
  • 3
    When complaining about an error, it's a good idea to mention what the error is. Commented Apr 17, 2014 at 15:25
  • it is on the question now! thanx Commented Apr 17, 2014 at 15:30
  • @NilR Can you post the test instead of an image? Commented Apr 17, 2014 at 15:40

2 Answers 2

1

From the screen I would assume you should convert the u.EMP_ID, not the model.EMP_ID

You can also go with Int.Parse(model.EMP_ID)

To keep it readable and efficient I would go with:

int modelID = Int.Parse(model.EMP_ID);
var user = db.EMPLOYEEs.Where(u => u.EMP_ID == modelID && u.EMP_PASSWORD == model.EMP_PASSWORD).FirstOrDefault();

First of all, you parse only once. Secondly, you compare ints which is a lot faster.

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

6 Comments

thank you I get another error down on serializeModel.roles = user.RoleROLE_ID; error: Cannot implicitly convert type 'int' to 'string' what I suppose to do with this one?
@NilR either: serializeModel.roles.ToString() or Int.Parse(user.RoleROLE_ID). The first option should be faster if you set an additional variable to hold result for that.
I can't the first one give me errors and don't take them :(
@NilR edit your question to provide more info on that or create new one. I don't know what happens from such a short description.
@NilR you try to assign a number to an array. Do you want to add this int to the array or what?
|
0

just pull that conversion out of the Where clause:

string empID = Convert.ToString(model.EMP_ID);
var user = db.EMPLOYEEs
             .Where(u => u.EMP_ID == empID && u.EMP_PASSWORD == model.EMP_PASSWORD)
             .FirstOrDefault();

2 Comments

it didn't have at the first place before error and I changed to this one but still doesn't work...
i did this : int modelID = Int.Parse(model.EMP_ID); var user = db.EMPLOYEEs.Where(u => u.EMP_ID and worked fine; however I have another error that down on serializeModel.roles = user.RoleROLE_ID; error: Cannot implicitly convert type 'int' to 'string' what I suppose to do with this one?

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.