in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

DataAdapter.Fill exception: "String was not recognized as a valid DateTime"

Last post 04-10-2012 11:45 AM by michaelgoldstone. 46 replies.
Page 1 of 4 (47 items) 1 2 3 4 Next >
Sort Posts: Previous Next
  • 07-27-2006 1:25 AM

    DataAdapter.Fill exception: "String was not recognized as a valid DateTime"

    Hi everybody, I'm having this error while executing a simple "SELECT * from TableX" on a SqLite 3 database. The DB was created with ormb2k.de SQLite admin, and a single record is in the table. Looks like the problem are the DateTime fields, which were filled automatically since the table has some defaults (CURRENT_TIMESTAMP). If I query some of the fields, excluding the DateTime fields, everything is OK, but if I do "select * .." the DataAdapter.Fill method invariably generates the exception? Can anybody help me? Is there some wrong data written in the db (maybe by the interface), some known bug or something specific to do ? I'm using the latest 1.0.31 provider inside VC# express. Thank you!
  • 07-27-2006 2:14 AM In reply to

    Re: DataAdapter.Fill exception: "String was not recognized as a valid DateTime"

    dzingoni:
    Hi everybody, I'm having this error while executing a simple "SELECT * from TableX" on a SqLite 3 database. The DB was created with ormb2k.de SQLite admin, and a single record is in the table. Looks like the problem are the DateTime fields, which were filled automatically since the table has some defaults (CURRENT_TIMESTAMP). If I query some of the fields, excluding the DateTime fields, everything is OK, but if I do "select * .." the DataAdapter.Fill method invariably generates the exception? Can anybody help me? Is there some wrong data written in the db (maybe by the interface), some known bug or something specific to do ? I'm using the latest 1.0.31 provider inside VC# express. Thank you!


    Just more info ....
    The fields where created with the ORbm2k SQLite administrator as TIMESTAMP, with a default of CURRENT_TIMESTAMP. Let's forget about the fact that the administrator has a bug ... and when you edit the table the default is changed to the actual value ... well, the problem (that's my impression) is that if you have a default value of CURRENT_TIMESTAMP the database engine actually generates that ... but using the local datetime format (I'm in Italy, so that's not the required ISOXXX format, of course), and then (it's still my guess) the SQLite engine treates the value as a string ... to be short, the wrong format is used.
    If this is the case, how can I use a default of CURRENT_TIMESTAMP for TIMESTAMP fields if the system generates wrong format vaues? Am I doing something wrong, I got the problem, or am I completaly on the wrong way?
    Thank you!
  • 07-27-2006 3:05 AM In reply to

    Re: DataAdapter.Fill exception: "String was not recognized as a valid DateTime"

    Please, could you post the code you are using? I have made the following test and I could not reproduce the problem.

    1. Created a new SQLite 3 database using SQLite Administrator 0.8.2.5 beta.
    2. Created the following table called Table1 using SQLite Administrator:
      • Id INTEGER PRIMARY KEY
      • DateTimeField TIMESTAMP DEFAULT CURRENT_TIMESTAMP
      • StringField TEXT
    3. Inserted two records using "Edit Data" tab of SQLite Admin, typing only data for StringField
    4. Wrote and run the following code with no exceptions. All seems to work correctly:

        private void TimeStampTest()
        {
          using (SQLiteConnection cn = new SQLiteConnection("Data Source=TimestampTest.s3db"))
          using (SQLiteCommand GetRecordsCmd = new SQLiteCommand("SELECT Id, DateTimeField, StringField FROM Table1", cn))
          {
            cn.Open();
            using (SQLiteDataReader reader = GetRecordsCmd.ExecuteReader())
            {
              while (reader.Read())
              {
                long Id = reader.GetInt64(0);
                DateTime DateTimeField = reader.GetDateTime(1);
                string StringField = reader.GetString(2);
                Debug.WriteLine(String.Format("Id: {0}, DateTimeField: {1}, StringField: {2}", Id, DateTimeField, StringField));
              }
            }

            using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(GetRecordsCmd))
            {
              DataTable table = new DataTable("Table1");
              adapter.Fill(table);
              this.dataGridView1.DataSource = table;
            }
          }
        }

    Did you do something different ? Perhaps you typed the dates in italian format?

    Incidentally,  my current  regional  settings are spanish.  The date format is : DD/MM/YYYY


    Regards

    Jesús López

  • 07-27-2006 4:18 AM In reply to

    Re: DataAdapter.Fill exception: "String was not recognized as a valid DateTime"

    Thank you for the reply. I'm actually still investigating the problem. The code is simple: DataAdapter.Fill(...) it is supposed to retrieve the datatable structure from the db, but it generates the exception. The problem lies in the actual data written in the db. I have some records written correctly, some not. if I execute the following SQL (always using SQL Administrator): SELECT DateTime(LastUpdated), DateTime(ExpiresOn) from Objects where the two fields LastUpdated and ExpiresOn are TIMESTAMP with CURRENT_TIMESTAMP as default, I get some records with the date, some empty. If I browse to the table I see everything, all records have the timestamps with various values, but if I query ... something looks impossible to convert to DateTime. Initially I thought the problem was with the date format (when inserting) but if I try to input some wrong data the SQL Administrator complaints, so there are checks and conversions (I insert the dates in the Italian format, the SQL Administrator wants them that way, in other words it uses the International settings of my machine). Actually I don't see much difference between values that convert and values that do not. I though about some details like leading zeroes .. but nothing. Is there a way to see the STRING that is actually recorded in the database file?
  • 07-27-2006 4:30 AM In reply to

    Re: DataAdapter.Fill exception: "String was not recognized as a valid DateTime"

    I think I have tracked down the problem to this.

    I tried several SQL statements (from the SQLite Administrator interface) , like:

    SELECT CURRENT_TIMESTAMP

    SELECT DateTime(CURRENT_TIMESTAMP)

    SELECT FieldName

    SELECT DateTime(FieldName)

    OK, the first gives the current time in the SQLite format YYYY-MM-DD HH:mm:ss
    The second writes the same (? I don't understand, but ...)
    The third gives the actual data of the field as string
    THE FOURTH ... sometimes gives nothing, sometimes gives the ITALIAN FORMATTED data

    So in my opinion the problem (problem?) lies in the DateTime function that actually fails sometimes the conversion. Probably inside VC# this generates an exception (the infamous "String was not recognized as a valid DateTime") , while in SQLite administrator it is masked by the software.

    Now the problem is, it's not ME who is inserting wrong data in the field, it's the DEFAULT CURRENT_TIMESTAMP ! I don't have any control on it ....
     


  • 07-27-2006 4:35 AM In reply to

    Re: DataAdapter.Fill exception: "String was not recognized as a valid DateTime"

    SQLiteAdminIn simple words, here's the query and result:

    SELECT CURRENT_TIMESTAMP, DateTime(CURRENT_TIMESTAMP), LastUpdated, DateTime(LastUpdated) from Objects
       

     .Column0 {FONT-FAMILY: Tahoma; FONT-SIZE: 10pt; TEXT-ALIGN: left; COLOR: #000000; BACKGROUND: #FFFFFF;} .Column1 {FONT-FAMILY: Tahoma; FONT-SIZE: 10pt; TEXT-ALIGN: left; COLOR: #000000; BACKGROUND: #FFFFFF;} .Column2 {FONT-FAMILY: Tahoma; FONT-SIZE: 10pt; TEXT-ALIGN: left; COLOR: #000000; BACKGROUND: #FFFFFF;} .Column3 {FONT-FAMILY: Tahoma; FONT-SIZE: 10pt; TEXT-ALIGN: left; COLOR: #000000; BACKGROUND: #FFFFFF;}
    CURRENT_TIMESTAMPDateTime(CURRENT_TIMESTAMP)LastUpdatedDateTime(LastUpdated)
    2006-07-27 11:31:022006-07-27 11:31:0226/06/2006 10.03.342006-06-26 10:03:34
    2006-07-27 11:31:022006-07-27 11:31:0226/06/2006 16.04.32 
    2006-07-27 11:31:022006-07-27 11:31:0227/07/2006 06.21.03 
    2006-07-27 11:31:022006-07-27 11:31:0227/07/2006 11.00.41 

    As you can see CURRENT_TIMESTAMP works, DateTime(CURRENT_TIMESTAMP) correctly interprets that values, but if the data is stored in the DB (3rd column) and then reinterpreted by DateTime .... most of the times I get nothing!
  • 07-27-2006 4:59 AM In reply to

    Re: DataAdapter.Fill exception: "String was not recognized as a valid DateTime"

    dzingoni:
    . The problem lies in the actual data written in the db. I have some records written correctly, some not. if I execute the following SQL (always using SQL Administrator): SELECT DateTime(LastUpdated), DateTime(ExpiresOn) from Objects where the two fields LastUpdated and ExpiresOn are TIMESTAMP with CURRENT_TIMESTAMP as default, I get some records with the date, some empty.


    It sounds like somebody updated the fields with strings in italian format. Are you using parametrized queries to do the updates or are you concatenating strings to build the CommandText?

    If you concatenated the fields and used something like:

    CommandText = "UPDATE ...." + SomeDate.ToString() + ' .... '

    Then the filed is stored as a date formatted in the current regional settings.

    If you used parametrized queries, perhaps you set incorrectly the parameter type.

    dzingoni:
    Is there a way to see the STRING that is actually recorded in the database file?


    Yes, you can use DataReader.GetString() to see the stored strings. For example, (using the same table as before)

        private void ShowStrings()
        {
          DataTable Table1 = new DataTable("Table1");
          DataColumn IdCol = Table1.Columns.Add("Id", typeof(long));
          DataColumn DateTimeFieldCol = Table1.Columns.Add("DateTimeField", typeof(string));
          DataColumn StringFieldCol = Table1.Columns.Add("StringField", typeof(string));

          using (SQLiteConnection cn = new SQLiteConnection("Data Source=TimestampTest.s3db"))
          using (SQLiteCommand GetRecordsCmd = new SQLiteCommand("SELECT Id, DateTimeField, StringField FROM Table1", cn))
          {
            cn.Open();
            using (SQLiteDataReader reader = GetRecordsCmd.ExecuteReader())
            {
              while (reader.Read())
              {
                DataRow row = Table1.NewRow();
                row["Id"] = reader.GetInt64(0);
                row["DateTimeField"] = reader.GetString(1);
                row["StringField"] = reader.GetString(2);
                Table1.Rows.Add(row);
              }
            }
            Table1.AcceptChanges();
          }
          this.dataGridView1.DataSource = Table1;

        }


    Regards

    Jesús López

  • 07-27-2006 5:13 AM In reply to

    Re: DataAdapter.Fill exception: "String was not recognized as a valid DateTime"

    Thank you again.

    No, I actually didn't upodate anything.
    I just created a new record, filled the required NOT NULL fields by hand using the SQLite Administrator interface ... and posted . The values of the timestamp fields are generated by the DEFAULT statement in the definition of the table. That's all. So it's actually the system (SQLite.dll .. i think) that inserts the wrong data.

    Is there any special limitation about DEFAULT CURRENT_TIMESTAMP?
    I read that some features are experimental .....
  • 07-27-2006 6:30 AM In reply to

    Re: DataAdapter.Fill exception: "String was not recognized as a valid DateTime"

    One thing I haven't seen yet is the actual table schema.  Can you post the table's definition?

    Alternatively, if possible, can you e-mail me the database (zipped of course)? 

    robert at blackcastlesoft dot com

    Robert

     

  • 07-27-2006 6:42 AM In reply to

    Re: DataAdapter.Fill exception: "String was not recognized as a valid DateTime"

    Here is the table definition:

    CREATE TABLE "Objects" (
    "ObjectID" INTEGER  PRIMARY KEY AUTOINCREMENT NOT NULL,
    "MomObjectID" INTEGER  NOT NULL,
    "MomID" INTEGER  NOT NULL,
    "ObjectTypeId" INTEGER  NOT NULL,
    "OwnerId" INTEGER  NULL,
    "Name" VARCHAR(100)  NULL,
    "Description" VARCHAR(255)  NULL,
    "Notes" TEXT  NULL,
    "ExternalCode" VARCHAR(255)  NULL,
    "URL" VARCHAR(255)  NULL,
    "Path" VARCHAR(255)  NULL,
    "Active" BOOLEAN DEFAULT 'Y' NULL,
    "CreatedOn" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
    "ExpiresOn" TIMESTAMP DEFAULT '2099-01-01 00:00:00' NULL,
    "LastUpdated" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL
    )

    I'll send you the db with the table and data, so that you can peek inside ...
    Thank you in advance.
  • 07-27-2006 7:25 AM In reply to

    Re: DataAdapter.Fill exception: "String was not recognized as a valid DateTime"

    Robert, I just sent you the db ... let me know it it is blocked by some antispam engine somewhere ...
    Bye.
  • 07-27-2006 7:57 AM In reply to

    Re: DataAdapter.Fill exception: "String was not recognized as a valid DateTime"

    It might've been.  I  haven't received it yet.

     

  • 07-27-2006 1:33 PM In reply to

    Re: DataAdapter.Fill exception: "String was not recognized as a valid DateTime"

    I just sent the db again, this time it was zipped to avoid problems.
    File is rsimpson.zip.
    Thank you again. Daniele.
  • 07-27-2006 3:37 PM In reply to

    Re: DataAdapter.Fill exception: "String was not recognized as a valid DateTime"

    I'm getting the same error/behavior. If Daniele's sample doesn't show enough to trace the problem I can send sample code too.
  • 07-27-2006 6:53 PM In reply to

    Re: DataAdapter.Fill exception: "String was not recognized as a valid DateTime"

    I still haven't received it.  You sent it to robert@blackcastlesoft.com  right?

     

Page 1 of 4 (47 items) 1 2 3 4 Next >
Powered by Community Server (Commercial Edition), by Telligent Systems