'How to handle a DateTime exception of "System.Globalization.GregorianCalendar" while reading a JSON text file in Windows Form(C#)

How to handle a Date & Time exception of "System.Globalization.GregorianCalendar" while reading a JSON text file (Date Value is : "00000000") in Windows Form??

In my code I am reading a Json data in a Text file which is coming from a device of my company. The Json data in each line looks like this after formatting :

{
  "version": "2.24",
  "imei": "352953083329635",
  "chg": "1678",
  "sts": "0",
  "art": "6",
  "rows": [
    {
      "lat": "E93E",
      "lon": "0C4A2702",
      "tim": "20220511095303",    // TimeStamp
      "course": "NE",
      "speed": "0.848",
      "alt": "63.0",
  ]
} 

I am making a tool to shows the whole Json data in the text file in a "DataGridView" along with "Date & Time" filter and "Version" filter. And then exporting the data in the GridView as a .csv File.

Since my data is coming from a device its more prone to recieve garbage data. Once such data I am receiving in "TimeStamp" part. It some time receives, data as "000000000".

"tim":"00000000000000" // Like this.

I'm not able to find a solution to this Error in general searching in Google & "StackoverFlow", Please help me!

My code :

public DataTable AddRow(DataTable newTable, string data)
    {
        
        var jsonSerializerSettings = new JsonSerializerSettings();
        jsonSerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;
      
        Root myC = JsonConvert.DeserializeObject<Root>(data, jsonSerializerSettings);
        String version = myC.Version;
        String imei = myC.Imei;
        String charging = myC.Charging;
        String status = myC.Status;
        String alert = myC.AlertFlag;
        String lat = myC.rows[0].Lat;
        String lon = myC.rows[0].Lon;
        String dtString = myC.rows[0].TimeStamp;
        String direction = myC.rows[0].Direction;
        String speed = myC.rows[0].Speed;
        String altitude = myC.rows[0].Altitude;
       
        if(version == "" || imei == "" || charging == "" || status == "" || alert == "" || lat == "" || lon == "" || dtString == "" ||
            speed == "" || altitude == "" )
        {
            return newTable;
        }

        DateTime dt = DateTime.ParseExact(dtString, "yyyyMMddHHmmss",null); // Error here
       
        newTable.Rows.Add(version, imei, charging, status, alert, lat,lon, dt, 
            direction, speed, altitude);
        return newTable;
    }

(Its while adding rows to Table while deserialzing, I'm calling the table, while reading the each line of file in "FileReader",to add them to table for DataSource) Like this:

private void showData_Click(object sender, EventArgs e)
     {
          dataGridView1.DataSource = null;
          try
          {
          if (File.Exists(textfile))
          {
            using (StreamReader file = new StreamReader(textfile2))
            {
             DataTable newTable = new DataTable();

             newTable.Columns.Add("TimeStamp", typeof(DateTime));
             (Similarly other declartions for other columns)   

             while ((ln = file.ReadLine()) != null)
                    {
                     // Some Conditions are here
                     continue;
                     }
                        newTable = AddRow(newTable, ln);
                    }                    
            }
            file.Close();
            dataGridView1.DataSource = newTable;
        }
    }
}
catch (Exception ex)
{
    txtError.Text = "ex:>>" + ex.ToString();
}        

}



Solution 1:[1]

Add this as a script:

function onOpen() {
  SpreadsheetApp.getUi().createMenu("Refresh Formulae").addItem("Refresh", "refresh").addToUi()
}

function refresh() {
  const ss = SpreadsheetApp.getActiveSpreadsheet()
  const sheet = ss.getSheetByName("report overview")
  const range = sheet.getRange("A2:B2")
  
  range.setFormulas(range.getFormulas())
}

Rundown:

  • On open, create a menu item to refresh the formulae
  • Once authorised and run, this will re-set the formulae in A2 and B2

References:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 I hope this is helpful to you