Specified cast is not valid

2004-04-15
2004-04-17
  • Nobody/Anonymous

    I have a table created with :

    CREATE TABLE Jobs
    (IdJob INTEGER PRIMARY KEY,
    Title NVARCHAR(255)
    LastRun DATETIME)

    I have several records in this table and I can't read/load data when I make a search with :

    SELECT * FROM Jobs WHERE Title = 'Test'

    My code :

    IDataReader dr = myCmd.ExecuteReader(CommandBehavior.CloseConnection);
    if (dr.Read() == true) {
      _IdJob = (int) dr["IdJob"];
      _Title = (string) dr["Title"];
      _LastRun = (DateTime) dr["LastRun"];
    }

    And I have error Specified cast is not valid. when running _IdJob = (int) dr["IdJob"];.

    When I comment _IdJob = (int) dr["IdJob"]; and _LastRun = (DateTime) dr["LastRun"];, I have no error with _Title = (string) dr["Title"];.

    Any hints ?
    Michel.

     
    • Tim McDaniel

      Tim McDaniel - 2004-04-15

      I think I know what the problem is.  Looks like a bug in DataReader.cpp.

      In DataReader.cpp, try changing this:
      ------------------------------------------
      Object* SQLiteDataReader::get_Item (int i)
      {
          if( mFieldValues[i] == 0 )
              return DBNull::Value;
          else
              return MarshalStr::FromSQLite (mFieldValues[i]);
      }
      ------------------------------------------
      to this:
      ------------------------------------------
      Object* SQLiteDataReader::get_Item (int i)
      {
          GetValue (i);
      }
      ------------------------------------------
      and recompile SQLite.NET.

       
    • Tim McDaniel

      Tim McDaniel - 2004-04-15

      Oops, make that:
      ------------------------------------------
      Object* SQLiteDataReader::get_Item (int i)
      {
      return GetValue (i);
      }
      ------------------------------------------

       
      • Alexander Gavrilov

        I updated CVS with the suggested change.

         
    • Wilson Chan

      Wilson Chan - 2004-04-16

      I created that table, and execute:
      INSERT INTO Jobs LastRun VALUES('2004')

      it can no longer be run correctly even though I restart the program: "Null Reference: that string could not be recognized as a valid DateTime"

       
      • Alexander Gavrilov

        I believe the string "2004" is not valid value for DataTime type in C#. Remember SQLite is typeless and keeps all data as strings no matter what type is used in the table schema. That's why you were be able to store the invalid value in LastRun column. SQLite.NET, on the other hand, tries to convert column values to valid C# types before it returns the value from SQLiteDataReader.get_Item or GetValue. SQLite.NET is using Parse function from the corresponding C# type to convert values from the string. But DateTime.Parse("2004") throws the exception "that string could not be recognized as a valid DateTime". That's why you're getting the exception you described.

         
    • Wilson Chan

      Wilson Chan - 2004-04-17

      so, is there still any method to return the value without throwing exceptions?

       
    • Wilson Chan

      Wilson Chan - 2004-04-17

      so, is there still any method to return the value without throwing exceptions?

       
      • Alexander Gavrilov

        Try use GetString like this:

        rd.GetString(rd.GetOrdinal("LastRun"));

        Then you can parse the returned string whatever you like.

         

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks