Assign 'null' value to a string.Format

Rashmitha B 0 Reputation points
2024-03-28T16:30:48.9133333+00:00

Source code:

sr.Body.RequestBody = string.Format

                                           ((string)configData.PostFSObj, obj1.AiConfiguration, obj1.AircraTypeIndex, obj1.Arrivadelaytime,

                                            obj1.ATAVOYAGEREPORT, obj1.ATDVOYAGEREPORT,

                                            obj1.Carrier, obj1.Departdelaytime, obj1.FlightLegCode, obj1.FlightNumber,

                                            obj1.OperationDay, obj1.Sctimeofarrival, obj1.timeofdeparture

                                            , obj1.TotalPassengers, dsStrProp, asStrProp, regStrProp);  
```CustomConfig.CommonFactory.WriteToFile("Request Obj => " + sr.Body.RequestBody, pr.ProcessID);

I have a result for obj1 as below:

"AiConfiguration": "Scheduled Service", "AircraTypeIndex": "7M8", "Arrivadelaytime": "", "ATAVOYAGEREPORT": null, "ATDVOYAGEREPORT": null, "CarrierIndex": "FZ", "Departdelaytime": null, "FlightLegCode": " ", "FlightNumber": 27, "OperationDay": "2024-03-28T00:00:00+05:30", "Sctimeofarrival": "2024-03-28T09:00:00Z", "timeofdeparture": null, "TotalPassengers": 0,  
  
but after convverting to string format:

 sr.Body.RequestBody : ATAVOYAGEREPORT : "","ATDVOYAGEREPORT:""  
I want sr.Body.RequestBody result as it is like  ATAVOYAGEREPORT : null please help me.
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,250 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,571 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 112.1K Reputation points
    2024-03-28T17:14:07.6733333+00:00

    Instead of obj1.ATAVOYAGEREPORT, try to specify obj1.ATAVOYAGEREPORT?.ToString( ) ?? "null".

    If it is a string, then use obj1.ATAVOYAGEREPORT ?? "null".


  2. Jiachen Li-MSFT 26,506 Reputation points Microsoft Vendor
    2024-03-29T08:29:24.8933333+00:00

    Hi @Rashmitha B ,

    sr.Body.RequestBody = string.Format(
        "{0}: {1}, {2}: {3}, {4}: {5}, {6}: {7}, {8}: {9}, {10}: {11}, {12}: {13}, {14}: {15}, {16}: {17}, {18}: {19}, {20}: {21}, {22}: {23}, {24}: {25}, {26}: {27}, {28}: {29}, {30}: {31}, {32}: {33}",
        "AiConfiguration", obj1.AiConfiguration,
        "AircraTypeIndex", obj1.AircraTypeIndex,
        "Arrivadelaytime", obj1.Arrivadelaytime,
        "ATAVOYAGEREPORT", obj1.ATAVOYAGEREPORT != null ? "\"" + obj1.ATAVOYAGEREPORT + "\"" : "null",
        "ATDVOYAGEREPORT", obj1.ATDVOYAGEREPORT != null ? "\"" + obj1.ATDVOYAGEREPORT + "\"" : "null",
        "Carrier", obj1.Carrier,
        "Departdelaytime", obj1.Departdelaytime,
        "FlightLegCode", obj1.FlightLegCode,
        "FlightNumber", obj1.FlightNumber,
        "OperationDay", obj1.OperationDay,
        "Sctimeofarrival", obj1.Sctimeofarrival,
        "timeofdeparture", obj1.timeofdeparture != null ? "\"" + obj1.timeofdeparture + "\"" : "null",
        "TotalPassengers", obj1.TotalPassengers,
        dsStrProp,
        asStrProp,
        regStrProp
    );
    
    CustomConfig.CommonFactory.WriteToFile("Request Obj => " + sr.Body.RequestBody, pr.ProcessID);
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments