System.Data.SQLite

Login
This project makes use of Eagle, provided by Mistachkin Systems.
Eagle: Secure Software Automation
2018-05-29
01:04 Ticket [baf42ee135] Unable to cast object of type 'System.DBNull' to type 'System.String' status still Closed with 3 other changes artifact: 518e16e143 user: mistachkin
2018-03-10
17:23 Closed ticket [baf42ee135]. artifact: 61cdb85141 user: mistachkin
09:49 Ticket [baf42ee135]: 3 changes artifact: 3617acce22 user: anonymous
2018-03-09
17:45 Ticket [baf42ee135]: 3 changes artifact: 786ae26a5f user: mistachkin
17:45
The GetSchemaTable method must verify the base table name (for a column) actually refers to a base table before attempting to query its metadata. Pursuant to [baf42ee135]. check-in: 63ae5401bf user: mistachkin tags: trunk
17:25
Add more tests pursuant to ticket [baf42ee135]. check-in: d1b6742e1e user: mistachkin tags: trunk
04:55 Ticket [baf42ee135] Unable to cast object of type 'System.DBNull' to type 'System.String' status still Pending with 3 other changes artifact: 05de8d38f7 user: anonymous
02:03 Ticket [baf42ee135]: 3 changes artifact: 7d8503af3e user: mistachkin
02:01 Ticket [baf42ee135]: 3 changes artifact: 9f7dd24596 user: mistachkin
01:57 Ticket [baf42ee135]: 3 changes artifact: 4e2fb359da user: mistachkin
01:49 Ticket [baf42ee135]: 3 changes artifact: 975d59ec41 user: anonymous
01:24 Ticket [baf42ee135]: 3 changes artifact: 3a8cda0a6a user: mistachkin
2018-03-08
22:58 Ticket [baf42ee135]: 3 changes artifact: cdba4c4e16 user: mistachkin
22:02 Ticket [baf42ee135]: 3 changes artifact: eabb0e43f5 user: anonymous
18:13
Prevent GetSchemaTable from throwing InvalidCastException. Fix for [baf42ee135]. check-in: c956230a3c user: mistachkin tags: trunk
16:13 Ticket [baf42ee135] Unable to cast object of type 'System.DBNull' to type 'System.String' status still Pending with 4 other changes artifact: a8de82b92b user: mistachkin
16:09
Add tests for ticket [baf42ee135] and get them passing. check-in: d0b0a52b9a user: mistachkin tags: tkt-baf42ee135
15:47 Ticket [baf42ee135] Unable to cast object of type 'System.DBNull' to type 'System.String' status still Pending with 3 other changes artifact: 473e1cf6e9 user: mistachkin
14:14
Improve handling of base catalog/table name in GetSchemaTable, pursuant to [baf42ee135]. check-in: 9f02226408 user: mistachkin tags: tkt-baf42ee135
14:03 Pending ticket [baf42ee135]: Unable to cast object of type 'System.DBNull' to type 'System.String' plus 7 other changes artifact: 30207ce07f user: mistachkin
13:56 New ticket [baf42ee135]. artifact: 5e829b8bd8 user: anonymous

Ticket Hash: baf42ee1359faa6407de2a57e33ce9849e8482b2
Title: Unable to cast object of type 'System.DBNull' to type 'System.String'
Status: Closed Type: Incident
Severity: Important Priority: Medium
Subsystem: Data_Reader Resolution: Fixed
Last Modified: 2018-05-29 01:04:50
Version Found In: 1.0.108.0
User Comments:
anonymous added on 2018-03-08 13:56:42:
System.InvalidCastException
  HResult=0x80004002
  Message=Unable to cast object of type 'System.DBNull' to type 'System.String'.
  Source=System.Data.SQLite
  StackTrace:
   at System.Data.SQLite.SQLiteDataReader.GetSchemaTable(Boolean wantUniqueInfo, Boolean wantDefaultValue) in x:\SQLiteDataReader.cs:line 1493

// Get the column meta data
          _command.Connection._sql.ColumnMetaData(
            (string)row[SchemaTableOptionalColumn.BaseCatalogName],
            (string)row[SchemaTableColumn.BaseTableName],
            strColumn,
            ref dataType, ref collSeq, ref bNotNull, ref bPrimaryKey, ref bAutoIncrement);

By default, BaseTableName and BaseCatalogName should be set to an empty string to prevent throwing the exception.
row[SchemaTableColumn.BaseTableName] = "";
row[SchemaTableOptionalColumn.BaseCatalogName] = "";

mistachkin added on 2018-03-08 14:03:29:
Can you provide the database schema involved in this issue?  It could be quite
helpful.

mistachkin added on 2018-03-08 16:13:47:
I've created an initial test case that is able to reproduce the exception;
however, in order to be confident in the fix, I would really still like to
see the original schema that triggered the issue.

anonymous added on 2018-03-08 22:02:42:
https://github.com/ErikEJ/SqlCeToolbox/issues/641

mistachkin added on 2018-03-08 22:58:52:
Thanks for the information.

Are you able to test the changes on trunk to see if they fix the issue in your
environment?

mistachkin added on 2018-03-09 01:24:10:
I've tried using the SQL from the linked GitHub issue and it does not cause the
exception.

anonymous added on 2018-03-09 01:49:03:
The problem has been fixed but there is still one issue when I try to get the ViewColumn of vwGroupBibs.

System.Data.SQLite.SQLiteException
  HResult=0x800007BF
  Message=SQL logic error
no such table column: tab.idGroup
  Source=System.Data.SQLite
  StackTrace:
   at System.Data.SQLite.SQLite3.ColumnMetaData(String dataBase, String table, String column, String& dataType, String& collateSequence, Boolean& notNull, Boolean& primaryKey, Boolean& autoIncrement) in x:\SQLite3.cs:line 2099


The following schema is from the file test.db of the linked GitHub
CREATE TABLE [GroupBibs] ( 
  [idGroupBib] uniqueidentifier NOT NULL 
, [idGroup] uniqueidentifier NOT NULL 
, [BibFrom] bigint  NOT NULL 
, [BibTo] bigint  NOT NULL 
, CONSTRAINT [sqlite_autoindex_GroupBibs_1] PRIMARY KEY ([idGroupBib]) 
);

CREATE VIEW [vwGroupBibs] AS 
WITH RECURSIVE 
    [tab] AS( 
        SELECT [BibFrom],  
               [BibTo],  
               [idGroup] 
        FROM   [GroupBibs] 
        UNION ALL 
        SELECT [BibFrom] + 1,  
               [BibTo],  
               [idGroup] 
        FROM   [tab] 
        WHERE  [BibFrom] < [BibTo] 
    ) 
SELECT DISTINCT [idGroup] AS [idGroup],  
       [BibFrom] AS [BibNumber] 
FROM   [tab]

mistachkin added on 2018-03-09 01:57:45:
This appears to be a different issue.  I will look into it.

For the original issue, do you have any additional SQL that I might be able to
try and replicate it?

mistachkin added on 2018-03-09 02:01:32:
Interestingly, the new SQL causes the InvalidCastException to be raised when run
against 1.0.108.0; however, it does not appear to cause any exception when run
against trunk.

mistachkin added on 2018-03-09 02:03:05:
Ok, it *does* cause the new exception when querying for VIEWCOLUMNS; however,
it does not cause any exception when querying for COLUMNS.

anonymous added on 2018-03-09 04:55:11:
You can use the following code to reproduce this issue:

using (SQLiteConnection conn = new SQLiteConnection(@"data source=""x:\test.db"""))
{
    conn.Open();
    using (DataTable dt = conn.GetSchema("Columns", new string[]{null, null, "vwGroupBibs", null}))
    {
        MessageBox.Show(dt.Rows.Count.ToString()); //No Exception but result is empty.
    }
    using (DataTable dt = conn.GetSchema("ViewColumns", new string[] { null, null, "vwGroupBibs", null }))
    {
        MessageBox.Show(dt.Rows.Count.ToString()); //Throwing the following error message.
        /*
        System.Data.SQLite.SQLiteException
          HResult=0x800007BF
          Message=SQL logic error
        no such table column: tab.idGroup
          Source=System.Data.SQLite
          StackTrace:
           at System.Data.SQLite.SQLite3.ColumnMetaData(String dataBase, String table, String column, String& dataType, String& collateSequence, Boolean& notNull, Boolean& primaryKey, Boolean& autoIncrement) in x:\SQLite3.cs:line 2099
        */
    }
}

mistachkin added on 2018-03-09 17:45:55:
Changes on trunk should fix the remaining issues reported here.  Please let us
know if they work for you.

anonymous added on 2018-03-10 09:49:38:
Thanks! Now everything is working fine but this issue.

using (SQLiteConnection conn = new SQLiteConnection(@"data source=""G:\My CodeLib\test1.db"""))
{
    conn.Open();
    using (DataTable dt = conn.GetSchema("Columns", new string[]{null, null, "vwGroupBibs", null}))
    {
        foreach (DataRow dr in dt.Rows)
        {
            System.Console.WriteLine(dr["DATA_TYPE"].ToString());
            //idGroup --> uniqueidentifier --> ok
            //BibFrom --> "" --> Unable to return the datatype 'bigint'
        }
    }
}

mistachkin added on 2018-03-10 17:23:59:
Unfortunately, given how the view is built, the metadata for the columns is not
generally available.  That the "idGroup" column returns valid metadata is not a
documented feature as much as it is an internal implementation detail, which may
change in the future.  In general, these types of calculated columns do not have
associated metadata.

The "no such column" bug here was that the SQLiteDataReader class was attempting
to query the metadata for non-table columns.  That has now been fixed.

mistachkin added on 2018-05-29 01:04:50:
Check-in [c956230a3c387977].