none
How to have IF condition with HttpClient POST? RRS feed

  • Question

  • Hi,

    I am using below code to post using HttpClient.

    I want to know how can I have an IF condition within that? for example, if the latitude is null then make it "" [empty string]

    here is my code:

    var client = new HttpClient();
    
    client.BaseAddress = new Uri("http://www.domain.com/ws/signup.php");
    
    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("first_name", EntrySignupFirstName.Text),
        new KeyValuePair<string, string>("last_name", EntrySignupLastName.Text),
        new KeyValuePair<string, string>("gender", GenderCode),
        new KeyValuePair<string, string>("country", CountryCode),
        new KeyValuePair<string, string>("email", EntrySignupEmailAddress.Text),
        new KeyValuePair<string, string>("password", EntrySignupPassword.Text),
        new KeyValuePair<string, string>("createdcountry", current_country),
        new KeyValuePair<string, string>("latitude", Convert.ToString(latitude)),
        new KeyValuePair<string, string>("longitude", Convert.ToString(longitude))
    });
    

    Thank you,

    Jassim

    Saturday, June 23, 2018 7:46 PM

Answers

All replies

  • Hi Jassim Rahma,

    Thank you for posting here.

    For your question, try to foreach the content and get the value of latitude. And then check the value of latitude. If the latitude is null, make it empty string.

    If you want the more help with code samples, please provide details about your code for us to test. 

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 25, 2018 7:46 AM
    Moderator
  • Is it possible to have it within this line:

        new KeyValuePair<string, string>("country", CountryCode),
    

    Monday, June 25, 2018 11:16 AM
  • Lookup the ?: operator.

    And the coalescing operator

    E.g

    new KeyValuePair<string, string>("latitude", latitude==null? "" : Convert.ToString(latitude) )

    or

     new KeyValuePair<string, string>("country", CountryCode ?? ""),


    • Edited by RJP1973 Monday, June 25, 2018 1:26 PM
    • Marked as answer by Jassim Rahma Monday, June 25, 2018 2:20 PM
    Monday, June 25, 2018 1:25 PM