0

attempting to run a very simple query and fill a datatable, but keep getting a "Failed to enable constraints..." error (using mysql ver 8.0.13). Here's the relevant code:

dim dt as new datatable
Using mycon As New MySqlConnection(...)
            Using mycmd As New MySqlCommand("Select * from output_type where 
              is_active='Y';", mycon)
            Try
                mycon.Open()
                   using myread as mysqldatareader = mycmd.executereader
                     dt.load(myread)
                   end using
                mycon.Close()
                End Using
            Catch ex As Exception
            Finally
                If mycon.State <> ConnectionState.Closed Then
                    mycon.Close()
                End If
            End Try
        End Using
    End Using
return dt

I am absolutely certain there are no null values, foreign keys, etc in the output_type table...it is only 9 rows!! What could be causing the error.

Thanks for the help, Daniel

  • Please add to your post with your table schema – Ryan Wilson Jan 10 '19 at 18:25
  • We need more information as @RyanWilson suggested. But, your error implies that there is a pre-existing foreign key constraint between the table `output_type` and another table that requirements hasn't been met. – Fraze Jan 10 '19 at 18:32
  • On which line does the error appear? You could remove the empty catch statement since you are already implementing Using – the_lotus Jan 10 '19 at 18:39
  • Here's the schema for the table: – daniel tucker Jan 10 '19 at 19:44
  • intake_index int(3) NO PRI auto_increment intake_type varchar(200) NO is_active char(1) YES Y – daniel tucker Jan 10 '19 at 19:44
  • There is no foreign key constraint anywhere for this table. It's just a simple "pick-list" table used to fill a dropdownlist. There are no nulls, no foreign key constraints, no-joins. Baffling! – daniel tucker Jan 10 '19 at 19:49
  • I have resolved the error with the following code, but I consider it a bit of a hack and would rather fix the underlying cause with the datatable. Code in next comment: – daniel tucker Jan 10 '19 at 19:51
  • Using dt As New DataTable Dim myread As MySqlDataReader = mycmd.ExecuteReader Using ds As New DataSet ds.EnforceConstraints = False ds.Tables.Add(dt) dt.Load(myread, LoadOption.OverwriteChanges) ds.Tables.Remove(dt) End Using mycon.Close() Return dt – daniel tucker Jan 10 '19 at 19:51
  • 1
    Possible duplicate of [Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints](https://stackoverflow.com/questions/7026566/failed-to-enable-constraints-one-or-more-rows-contain-values-violating-non-null) – Ian Kemp Jan 11 '19 at 04:42

0 Answers0