Reputation: 245
I have a very freaky mistake...
I have two narrow-equals class, to extract data from sqlite Databases
I save dates as DATETIME in two SQLite databases. My first, contains this method to load datas
private void LoadData()
{
SetConnection();
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
string CommandText = "select ID,Name,Solde,Last_Update from Comptes";
sql_cmd.CommandText = CommandText;
SQLiteDataReader reader = sql_cmd.ExecuteReader();
while (reader.Read())
{
Compte compte = new Compte();
compte.ID = reader.GetInt16(reader.GetOrdinal("ID"));
compte.Nom = reader.GetString(reader.GetOrdinal("Name"));
compte.Solde = reader.GetDouble(reader.GetOrdinal("Solde"));
string to = reader.GetString(reader.GetOrdinal("Last_Update"));
DateTime dt=DateTime.ParseExact(reader.GetString(reader.GetOrdinal("Last_Update")),"yyyy-MM-dd hh:mm:ss",CultureInfo.InvariantCulture);
compte.Last_Update = dt;
this.Comptes.Add(compte);
}
sql_con.Close();
}
The second one:
private void ExecuteQuery(string txtQuery)
{
SetConnection();
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
sql_cmd.CommandText = txtQuery;
sql_cmd.ExecuteNonQuery();
sql_con.Close();
}
private void LoadData()
{
SetConnection();
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
string CommandText = "SELECT ID,Montant,ID_Emetteur,ID_Recepteur,Description,Date FROM Mouvements";
sql_cmd.CommandText = CommandText;
SQLiteDataReader reader = sql_cmd.ExecuteReader();
while (reader.Read())
{
Mouvement mouvement = new Mouvement();
mouvement.ID = reader.GetInt16(reader.GetOrdinal("ID"));
mouvement.Montant=reader.GetDouble(reader.GetOrdinal("Montant"));
mouvement.ID_Emetteur=reader.GetInt32(reader.GetOrdinal("ID_Emetteur"));
mouvement.ID_Recepteur=reader.GetInt32(reader.GetOrdinal("ID_Recepteur"));
mouvement.Description=reader.GetString(reader.GetOrdinal("Description"));
String out_date = reader.GetString(reader.GetOrdinal("Date"));
try
{
DateTime dt = DateTime.ParseExact(out_date, "yyyy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture);
}
catch (Exception ex) { }
this.Mouvements.Add(mouvement);
}
sql_con.Close();
}
The first method works, but the second gives me an exception near the try catch. Format are exactly the same.
Hope someone will be able to help me !
Upvotes: 1
Views: 1122
Reputation: 16464
If you want to use 24-hour time you need to use HH in your format string. hh is for 12-hour time and the supplied time is > 12, so an exception is thrown. This will work.
DateTime dt = DateTime.ParseExact(out_date, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
Upvotes: 2