Results 1 to 8 of 8

Thread: Visual Studio 2017 - System.Data.ConstraintException

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    Visual Studio 2017 - System.Data.ConstraintException

    Dear all,
    I'm having some issue with my application.

    Basically I have a form where I'm initializing a datagridview. From this form I'm the loading another form (without closing the first form) where I have another datagridview which loads data from the same table of the other datagridview.

    Here below the code of the first form:

    Private Sub Analisi_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'CTO_ManagementDataSet7.Tbl_CTOPartiteOss' table. You can move, or remove it, as needed.
    Me.Tbl_CTOPartiteOssTableAdapter.Fill(Me.CTO_ManagementDataSet7.Tbl_CTOPartiteOss)
    'TODO: This line of code loads data into the 'CTO_ManagementDataSet6.Tbl_CTOSquadre' table. You can move, or remove it, as needed.
    Me.Tbl_CTOSquadreTableAdapter.Fill(Me.CTO_ManagementDataSet6.Tbl_CTOSquadre)

    DtGrdSquadreOss.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    DtGrdPartiteOss.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect

    End Sub

    While here the code of the second one:

    Private Sub Details_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'CTO_ManagementDataSet.Tbl_CTOPartiteOss' table. You can move, or remove it, as needed.
    Me.Tbl_CTOPartiteOssTableAdapter.Fill(Me.CTO_ManagementDataSet.Tbl_CTOPartiteOss)

    End Sub

    Now, as said, when loading the second form I'm getting the following error: "System.Data.ConstraintException: 'Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.' "

    I believe it is because the first form is not closed. Any suggestion?

    Thanks,
    A.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Visual Studio 2017 - System.Data.ConstraintException

    Quote Originally Posted by AndreB82 View Post
    I believe it is because the first form is not closed.
    Why's that?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    Re: Visual Studio 2017 - System.Data.ConstraintException

    Dear jmcilhinney,
    basically in the first form I'm displaying all the records contained in the database table, while on the second one I'm displaying only a subset of them. In fact, the complete code for the second datagridview is the following:

    Code:
    Private Sub Details_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'CTO_ManagementDataSet.Tbl_CTOPartiteOss' table. You can move, or remove it, as needed.
            Me.Tbl_CTOPartiteOssTableAdapter.Fill(Me.CTO_ManagementDataSet.Tbl_CTOPartiteOss)
    
            Dim Filter As String
    
            Filter = Me.TxtDSCodSqua.Text
            Me.TblCTOPartiteOssBindingSource.Filter = "Codice_squadra Like '%" & Filter & "%'"
            Me.DtGrdDSPartiteOsservateSquadra.DataSource = TblCTOPartiteOssBindingSource
    
        End Sub

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Visual Studio 2017 - System.Data.ConstraintException

    Sounds like you have a DataSet with a Relationship setup between two tables with Constraints Enabled. This means you always have to Fill both tables (I believe the order you fill them is also import, I haven't done it in quite a while), so if form1 works then fill them the same way in form2. If you have a Master/Detail relation with constraints then there are things that happen when you modify or Add/Delete a record. If you Delete a record from the Master table then all the associated records in the Detail table must be deleted also. You can't Add a records to the Details table if there is no associated record already entered in the Master table first.

    Instead of filling both tables in separate forms, I prefer send the Details data to form2 as a parameter. There is more than one way to share data between form. I believe jmc has an example in the code bank and it should be easy to find lots of information on "passing data between forms".

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    Re: Visual Studio 2017 - System.Data.ConstraintException

    Dear wes4dbt,
    Probably I got your point wrongly, but I didn’t set up any Relationship between the two Dataset.
    Please advise if I have to further check to be sure of this.

    Thanks,
    A.

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Visual Studio 2017 - System.Data.ConstraintException

    Quote Originally Posted by AndreB82 View Post
    Dear wes4dbt,
    Probably I got your point wrongly, but I didn’t set up any Relationship between the two Dataset.
    Please advise if I have to further check to be sure of this.

    Thanks,
    A.
    Do you have 2 dastasets or did you mean 2 datatables? After looking closer at your first post I notice you seem to have multiple instances of the same dataset. Why?

    Me.CTO_ManagementDataSet7
    Me.CTO_ManagementDataSet6

    To check for relationships, open the dataset in the designer, if there is a relationship between to tables then you will see a line going from one to the other.

    One or more rows contain values violating non-null, unique, or foreign-key constraints.
    If you have no relationships with constraints then it could be there is no data in a field that requires all rows to have data in that field. But if your able to fill the datatable in form1 then you should be able to fill it in form2 unless you have done something to the datatable in form1 before trying to fill the datatable in form2.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    Re: Visual Studio 2017 - System.Data.ConstraintException

    Dear wes4dbt,
    I checked and I definitely don't have any relationship between tables and/or datasets.

    The reason of the two instances is due to the system. When I added the two datagridviews to the first form through the wizard they came out.
    Please do let me know if I have to correct anything.

    I checked even the table and all the fields required are filled in.

    A.

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Visual Studio 2017 - System.Data.ConstraintException

    There shouldn't be two instances of the dataset. The fact they are numbered 6 and 7 makes me believe you have added them several different times. I'd suggest getting rid of that form and add a new one then drag the two datagridviews onto the form from the datasource window. That's what I did hear.

    Code:
    Public Class Form8
        Private Sub BooksBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles BooksBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.BooksBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.BooksDBDataSet)
    
        End Sub
    
        Private Sub Form8_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'BooksDBDataSet.Author' table. You can move, or remove it, as needed.
            Me.AuthorTableAdapter.Fill(Me.BooksDBDataSet.Author)
            'TODO: This line of code loads data into the 'BooksDBDataSet.Books' table. You can move, or remove it, as needed.
            Me.BooksTableAdapter.Fill(Me.BooksDBDataSet.Books)
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Using frm As New Form9
                frm.ShowDialog()
            End Using
        End Sub
    End Class
    Code:
    Public Class Form9
        Private Sub AuthorBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles AuthorBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.AuthorBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.BooksDBDataSet)
    
        End Sub
    
        Private Sub Form9_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'BooksDBDataSet.Author' table. You can move, or remove it, as needed.
            Me.AuthorTableAdapter.Fill(Me.BooksDBDataSet.Author)
    
        End Sub
    End Class
    EDIT: I expanded my example to show you can use the same database table in multiple forms at the same time. Not suggesting you do that, just showing that's not the problem.
    Last edited by wes4dbt; Apr 4th, 2020 at 04:48 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width