Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug at DataTable.Load(dataReader) when result have NULL value on joined column #29744

Closed
eprechel opened this issue Dec 2, 2022 · 0 comments · Fixed by #32648
Closed

Bug at DataTable.Load(dataReader) when result have NULL value on joined column #29744

eprechel opened this issue Dec 2, 2022 · 0 comments · Fixed by #32648
Assignees
Labels
area-adonet-sqlite closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug

Comments

@eprechel
Copy link

eprechel commented Dec 2, 2022

When a query have a joined table and the join result have null values, but the column is a NOT-NULL-column, it throws a exception.
The same works with SQLiteDataAdapter.Fill from System.Data.SQLite

    private void Form1_Load(object sender, EventArgs e)
    {
      string filename = Path.Combine(Application.StartupPath, "test.db");
      if (File.Exists(filename))
      {
        File.Delete(filename);
      }

      conn = new SqliteConnection($"Data Source={filename};");
      conn.Open();

      DoJobs();
    }

    private void DoJobs()
    {
      string createsql = @"
          CREATE TABLE Member (
            ID INTEGER,
            Lastname TEXT NOT NULL,
            Firstname TEXT NOT NULL,
            Type INTEGER,
            Hidden INTEGER,
            PRIMARY KEY (ID AUTOINCREMENT)
          );

          CREATE TABLE Types (
            ID INTEGER,
            Description TEXT NOT NULL,
            Hidden INTEGER,
            PRIMARY KEY (ID AUTOINCREMENT)
          );
        ";

      SqliteCommand command = new SqliteCommand(createsql, conn);
      command.ExecuteNonQuery();

      string insertsql = @"
           INSERT INTO Types (Description) VALUES ('Administrator');
           INSERT INTO Types (Description) VALUES ('User');

           INSERT INTO Member (Lastname, Firstname, Type, Hidden) VALUES ('Mustermann', 'Max', 1, 0);
           INSERT INTO Member (Lastname, Firstname, Type, Hidden) VALUES ('Weber', 'Max', 2, 0);
           INSERT INTO Member (Lastname, Firstname, Type, Hidden) VALUES ('Müller', 'Willhelm', NULL, 0);
        ";

      command = new SqliteCommand(insertsql, conn);
      command.ExecuteNonQuery();

      string sql = @"
          SELECT 
            Member.ID AS ID,
            Member.Lastname,
            Member.Firstname,
            Types.ID AS TypeID,
            Types.Description AS Type,
            Member.Hidden
          FROM Member
          LEFT OUTER JOIN Types ON Types.ID = Member.Type
          WHERE Lastname <> 'Müller';
        ";

      SqliteDataReader dataReader;
      DataTable table = new DataTable();
      command = new SqliteCommand(sql, conn);
      dataReader = command.ExecuteReader();
      table.Load(dataReader);

      MessageBox.Show($"Datatable has {table.Rows.Count} rows in it");

      string sql1 = @"
          SELECT 
            Member.ID AS ID,
            Member.Lastname,
            Member.Firstname,
            Types.ID AS TypeID,
            Types.Description AS Type,
            Member.Hidden
          FROM Member
          LEFT OUTER JOIN Types ON Types.ID = Member.Type;
        "; // here is not WHERE Lastname <> 'Müller'; 

      table = new DataTable();
      command = new SqliteCommand(sql1, conn);
      dataReader = command.ExecuteReader();

      table.Load(dataReader); // error occurs here

      MessageBox.Show($"Datatable has {table.Rows.Count} rows in it");
    }

The same queries works fine if i use SqliteDataAdapter from System.Data.SQLite and fill the datatables with the same queries.
Another interesting thing is, if i insert a try-catch-block at the last table.Load(), it fills the datatable, like it schould be.

      try
      {
        table.Load(dataReader);
      }
      catch (Exception)
      {
      }
System.Data.ConstraintException
  HResult=0x8013192A
  Nachricht = Einschränkungen konnten nicht aktiviert werden. Mindestens eine Zeile enthält Werte die die Einschränkungen non-null, unique or foreign-key verletzen.
  Quelle = System.Data
  Stapelüberwachung:
   at System.Data.DataTable.EnableConstraints()
   at System.Data.DataTable.set_EnforceConstraints(Boolean value)
   at System.Data.DataTable.EndLoadData()
   at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
   at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
   at System.Data.Common.LoadAdapter.FillFromReader(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
   at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler)
   at System.Data.DataTable.Load(IDataReader reader)
   at BugInMicrosoftSqlite.Form1.DoJobs() in C:\Users\eugen\source\repos\BugInMicrosoftSqlite\Form1.cs:line 106

Version information

Microsoft.Data.Sqlite version: 7.0.0
Target framework: (e.g. .NET 4.8)
Operating system: Windows 11
Visual Studio 2022

@ajcvickers ajcvickers added this to the Backlog milestone Dec 5, 2022
@bricelam bricelam removed their assignment Jul 8, 2023
ajcvickers added a commit that referenced this issue Dec 19, 2023
@ajcvickers ajcvickers self-assigned this Dec 19, 2023
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Dec 19, 2023
@ajcvickers ajcvickers modified the milestones: Backlog, 9.0.0 Dec 19, 2023
@ajcvickers ajcvickers modified the milestones: 9.0.0, 9.0.0-preview1 Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-adonet-sqlite closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants