-2

I have tried to retrieve data from SQL database. I am using Entity Framework core. Its retrieving the required data from the database. I can see the data coming when debugging but the data is not assigning to the variable of type var. FYI, the value of variable type is 0 and its basically an enum, i typecast it to int. Below is the code

public async Task<string> GetMailTemplateByType(Models.TemplateTypes type)
    {
        var mailTemplate = await _userDbContext.MailTemplates.FirstOrDefaultAsync(mt => mt.TemplateType==((int)type));
        return mailTemplate.MailHtml;
    }

Here is the definition:

var HtmlTemplate = await _coreDataManager.GetMailTemplateByType(TemplateTypes.Activation);

when debug with try catch, Its showing

Object reference not set to an instance of an object

what is the problem here?

  • 1
    you could begin by checking the value of mailTemplate. – Anu Viswan Aug 03 '19 at 15:45
  • 1
    unless your ((int)type) is equal to 0 in the example above, it won't return the mailtemplate your show in your debugger – Rob Aug 03 '19 at 15:48
  • 1
    Are you sure, that you receiving any data? If linq doesn't find interested data, it responses with null, if string is null.. it is null. You should check if mailTemplate is not null (try{ ...}catch(NullReferenceException err){ Console.Writeline(err.Message);} if it shows you the same, you just catched null exception, and it does mean that you trying to find something that not exist. – Kamil Aug 03 '19 at 15:50
  • yes, mailTemplate is being null but i can see the data from my database as in the First screenshot – Nishanth Prabhakaran Aug 03 '19 at 16:07
  • 3
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – MickyD Aug 03 '19 at 16:12

3 Answers3

2
public async Task<string> GetMailTemplateByType(Models.TemplateTypes type)
{
  var mailTemplate = /*your expression from screenshot*/.FirstOrDefault();
  if(mailTemplate = null)
    throw new NullReferenceException();
  return mailTemplate;
}

..........................
try
{
  GetMailTemplateByType(TemplateTypesVariable);
}
catch(NullReferenceException err)
{
   Console.WriteLine("template does not exist");
}

It looks like you are trying to receive data which does not exist.

Kamil
  • 486
  • 6
  • 17
  • I can see the data populating as you can see in First screenshot. would be there any problem in FirstOrDefaultAsync(). – Nishanth Prabhakaran Aug 03 '19 at 16:25
  • 1
    ```public async Task``` but i dont understand, but what you want to return from this method? Own type, or string type? i understood that if its Task you expected string, so it is fix for exceptions like this, you need to use that to estabilish what exactly you want to return. Can you show me your entities? – Kamil Aug 03 '19 at 16:41
2

We can see from your code that you recieve the following mail template object: Id = {aeced541-7003-437e-8f77-4605766fb62c}; MailHtml = "Hi, 
Thank you so much for signing up.Here is Confirmation link to proceed further  ..."; TemplateType = 0;

Here you are passing some TemplateType value we don't know

public async Task<string> GetMailTemplateByType(Models.TemplateTypes type)
{

Here you compare that type value to the TemplateType property in the MailTemplate object we see in the dubugger window

var mailTemplate = await _userDbContext.MailTemplates.FirstOrDefaultAsync(mt => mt.TemplateType==((int)type));

But if type is not 0, it will not return the MailTemplate object as the MailTemplate object we see in the debugger window has a TemplateType value of 0, thus FirstOrDefaultAsync will return a null value, see "fault returns NullReferenceException if no match is found"

Rob
  • 568
  • 3
  • 9
  • Yes, the value you saw is from data base which is mt.TemplateType with value 0. the local value "type" is also 0 but its an enum so i typecasted that to int. – Nishanth Prabhakaran Aug 03 '19 at 16:12
  • Is it possible for you to provide the definition of your MailTemplate object and TemplateTypes enum to potentially better understand why the comparison might be failing? – Rob Aug 03 '19 at 16:28
  • @NishanthPrabhakaran based on your enum definition, you only have one enum value which is Activation = 1, how could you be passing a value of 0 to GetMailTemplateByType(Models.TemplateTypes type) when 0 isn't a valid value for TemplateTypes – Rob Aug 03 '19 at 16:38
  • 1
    You need to either change Activation to 0 in your enum or update the TemplateType in your database to 1 so they match – Rob Aug 03 '19 at 16:41
1

Why do you even select the whole object? ef-core is just like sql, select what you need (in your case just do a

var mailTemplate = await _userDbContext.MailTemplates.Where(mt => mt.TemplateType==((int)type)).Select(x => x.MailHtml).FirstOrDefaultAsync();

but this will still not work, since your entity says that the TemplateType is 0 (and your enum starts with 1). Guessing you saved it wrong

wanted to write this as a comment but i just created this account

sdi
  • 52
  • 3