Denmark Puso

Denmark Puso

  • NA
  • 232
  • 50.3k

How set value if there is no data in another table?

Sep 9 2019 1:11 AM
ill try to locate if my data is existing to other table and get the value of it. the problem is if there is no match in the other table i got an error but there's a match the controller save the value in my database. this is my code
 
and i got this error: Object reference not set to an instance of an object. if the "ReceiverCityId" has no value in "Location " i got an error. plsss help i need to set value into 0 if the "ReceiverCityId" has no value in "Location " 
  1. var rate = _rateService.GetRatebyParentId(Convert.ToInt32(pickup.Client.ParentClientId ?? 0));  
  2. var transaction = new Transaction  
  3. {  
  4.    Source = "Pickup",  
  5.    TransactionId = pickup.Id,  
  6.    Amount = (rate != null ? rate.RateItems.FirstOrDefault(x => x.Location ==   
  7.    _cityService.Get(pickup.ReceiverCityId).Name).Amount : 0) - 10,  
  8.    DateCreated = DateTime.Now,  
  9.    CreatedBy = User.Identity.Name,  
  10.    ReferenceNumber = pickup.ReferenceNumber  
  11. };  
  12. _transactionService.Add(transaction);

Answers (1)