1

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.

A Debugger Capture here

Hope someone will be able to help me !

3
  • What said the exception ? Commented Feb 6, 2014 at 22:42
  • Why are you getting the column out as string and then parsing it? Commented Feb 7, 2014 at 4:27
  • Because the GetDateTime method doesn't work for me. An exception happenned. Commented Feb 7, 2014 at 5:14

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.