Click here to Skip to main content
15,891,925 members

Get date value from SQLite Database

rune711 asked:

Open original thread
I give up.

I am trying to read a date value from a SQLite database. The column is defined as a DATETIME type. I sucessfully write the data to the database but when trying to read it throws the error: "
String was not recognized as a valid DateTime.
" My code looks like this:

C#
SQLiteConnection conn = new SQLiteConnection(this.ConnString);
            conn.Open();
            SQLiteCommand cmd = new SQLiteCommand(conn);
            cmd.CommandText = "SELECT * FROM tblAutomatedSearches";
            SQLiteDataReader reader = cmd.ExecuteReader();
            this.DBID = Convert.ToInt32(reader["ID"]);
            this.NotifyEmail = reader["notifyEmail"].ToString();
            this.SearchTitle = reader["jobTitle"].ToString();
            Object tempObject = reader["endDate"]; // <- throws the error
            string tempString = Convert.ToString(reader["endDate"]); //<- will throw the error also if above commented out


The date is in the format of "2012/06/11 12:51:14".

I understand that the SQLite engine actually stores the date value as a string. So it seems like the format that I write it in cant be read. I have tried EVERY format I can think of, e.g. MM/dd/yy, MM/dd/yyyy, M/d/yy, etc.

I am writing the date to the DB using a paramerterized query, ala':

C#
SQLiteConnection conn = new SQLiteConnection(this.ConnString);
            conn.Open();
            SQLiteCommand cmd = new SQLiteCommand(conn);
            cmd.CommandText = "INSERT INTO tblAutomatedSearches (jobTitle, endDate, notifyEmail, caseNumber) VALUES(@jobTitle,@endDate,@notifyEmail, @caseNumber)";
            cmd.Parameters.AddWithValue("@jobTitle", this.SearchTitle);
            cmd.Parameters.AddWithValue("@endDate", String.Format("{0:yyyy/MM/dd HH:mm:ss}", this.EndDate)); // <- this.EndDate is a DateTime type
            cmd.Parameters.AddWithValue("@notifyEmail", this.NotifyEmail);
            cmd.Parameters.AddWithValue("@caseNumber", this.CaseNumber);
            cmd.ExecuteNonQuery();


These inserts run just fine, so I'm left to think that the database is just entering the date as a string, but trying to read it as a date when retrieving it. I must be missing something. I will need to be able to sort based upon the date of these entries, so I can't just enter them as text.

You help is greatly appreciated.
Tags: C#, Sqlite

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900