Results 1 to 3 of 3

Thread: Failed Constraint??

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    207

    Failed Constraint??

    Hi guys,

    I've been having one hell of a night with a particular item. The code is as follows.

    Code:
        Public Function getClientsDeptCodes(ByVal user As String, ByVal password As String) As DataTable
            Dim dt As New DataTable
    
            command.CommandText = "SELECT Clients.ClientID, ClientName, DeptCode FROM Clients " & _
            "LEFT JOIN TollDepartmentCodes ON (Clients.ClientID = TollDepartmentCodes.ClientID);"
            command.CommandType = CommandType.Text
            command.Connection = connect.getConnection
    
            Try
                connect.connectionOpen(user, password, "DB")
                reader = command.ExecuteReader
                dt.Load(reader)
                connect.connectionClose()
            Catch ex As MySql.Data.MySqlClient.MySqlException
                errorsender.messageOut("clients", ex.Number, "getClientsDeptCodes", ex.Message)
                connect.connectionClose()
            End Try
    
            Return dt
        End Function
    Error on: dt.Load(reader)
    Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

    The table is as below

    CREATE TABLE IF NOT EXISTS Clients
    (
    ClientID VARCHAR(4) NOT NULL UNIQUE,
    ClientName VARCHAR(40) NOT NULL,

    PRIMARY KEY (ClientID)
    );

    CREATE TABLE IF NOT EXISTS TollDepartmentCodes
    (
    DeptCode VARCHAR(4) NOT NULL UNIQUE,
    ClientID VARCHAR(4) NOT NULL,

    PRIMARY KEY (DeptCode)
    FOREIGN KEY (ClientID) REFERENCES Clients (ClientID)
    );

    Item used here.

    Code:
            cboClient.DataSource = clients.getClients(frmLogin.txtUserName.Text, frmLogin.txtPassword.Text)
            cboClient.DisplayMember = "ClientName"
            cboClient.ValueMember = "ClientID"
            cboClient.SelectedIndex = -1
            cboClient.Text = ""
    When I run the query through MySQL command line, the query works fine. But VB.Net does not like it for some reason... Any help would be great. If you need more info I can provide. Thanks.
    Rate me if my response helped you. If not, I accept charity Rates as well.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    207

    Re: Failed Constraint??

    OK, I got it working, but I dont understand why I should DROP the PRIMARY KEY (DeptCode). If anyone could explain this for me leased I will know in future. Thanks.
    Rate me if my response helped you. If not, I accept charity Rates as well.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Failed Constraint??

    The error message tells you what the problem is:
    One or more rows contain values violating non-null, unique, or foreign-key constraints.
    So, instead of calling Load, populate the DataTable one row at a time and then it will fail on a specific row and you'll know exactly where the issue is.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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