Search code examples
c#-3.0sendgridsendgrid-api-v3sendgrid-templatessendgrid-api-v2

How to use sendGridAPI's last_event_time filter in queryParams json string with dynamic values in c#?


I have an error while using

'query':'last_event_time=2022-02-14T08:52:44Z'

it gives me unexpected identifier 'query' error, here is my code

var response = await client.SendEmailAsync(myMessage);  
var data = response.Headers.ToString();  
var splitData = data.Split("\r\n")[1].TrimStart('D', 'a', 't', 'e', ':').Replace("GMT", " ").Trim();  
var dateFormat = "ddd, dd MMM yyyy HH:mm:ss";  
DateTime emailSentDate;
bool date1 = DateTime.TryParseExact(splitData, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out emailSentDate);      
var lastEmailSent = emailSentDate.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssZ");  
var queryParams = @"{
      'query':'last_event_time"+lastEmailSent+"'"+","+"from_email:'"+From_Email+"'"+","+"subject:'"+myMessage.Subject+"'"+","+"limit:1"+"}";  

still this error was encounterd. I dont get exactly how to use 'query':'last_event_time="+lastEmailSent


Solution

  • Twilio SendGrid developer evangelist here.

    In your code you have 'query':'last_event_time"+lastEmailSent. I think you are missing an = in there. Each query item should also have quotes around it, like you have for the others, like from_email. You already had the quote after the lastEmailSent, but were missing =' before it. Try the string below.

    var queryParams = @"{
        'query':'last_event_time='"+lastEmailSent+"'"+","+"from_email:'"+From_Email+"'"+","+"subject:'"+myMessage.Subject+"'"+","+"limit:1"+"}";