0

// error this : object refrence not set to an instance of an object

// i want if user loging in site , user count==1 and user get rate==100 for first entry

[HttpPost]
        [Route("login/CustomerLogin")]
        public IActionResult CustomerLogin(string UserName, long Password)
        {
            try
            {
                var user = _repository.login.FindByCondition(c => c.Name == UserName && c.Password == Password)
                    .Select(c => new
                    {
                        c.ID,
                        c.Name,
                        c.Password,
                        c.Count,
                      
                    })


     .FirstOrDefault();
        var u = _repository.login.FindByCondition(c => c.Name == UserName && c.Password == Password).FirstOrDefault();

// error in this user.count condition

 if (user.Count != 0)
                {
                    int emt = 100;
                    _repository.login.Update(u);
                    _repository.Save();
                    return Ok(user);
                }
                else
                {
                    return BadRequest();
                }
            }
            catch (Exception e)
            {
                return BadRequest(e.Message);
            }
        }

    }

}
Helen
  • 87,344
  • 17
  • 243
  • 314
  • 1
    Have you debugged? Did you put a watch on the User object? Possibly the User object is null - and for that reason you cannot call a count on it. Instead maybe do a if(user !=null && user.count !=0) – Tomás Aug 15 '22 at 09:01
  • You could upvote my comment, please. Thanks – Tomás Aug 15 '22 at 10:42
  • `.FirstOrDefault()` returns the first match, *or a `null`* when nothing was found. So you need to watch out for that `null`! – Hans Kesting Aug 15 '22 at 13:20

0 Answers0