Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. error in VB.Net

杀马特。学长 韩版系。学妹 提交于 2019-12-21 20:43:50

问题


There were three similar questions in StackOverFlow but none gave an answer..

If have found why this error in occurring but don't know the fix.

I am using Strongly Typed Dataset for my project which is created as a dll for DAL.

I have added the Sql Server Table into this dataset using the designer and has created a DataAdapter

It works fine when i insert using DataTableAdapter

daLabTest.Insert(txtLabTestId.Text, cmbLabTestType.Text, cmbTestName.Text, txtLabFees.Text, dtpLabEffDate.Value)

but when i want to show the data from the table in a combobox or gridview i get this error.

i told that i found out what the problem is, I just previewed the data using DataSet designer and found out that the Function returns data like this...

The query i wrote to view this in dataset is

Select distinct(TestType) from LabTestTypes

so this should return only one column but the dataset is returning 5 columns but others as null, and the TestName column is a primary which should not be null when returned, so the problem exists..

To resolve this i tried to change the NullValue & AllowDBNull property to [Empty] and true respectively but that didn't worked for me.

Please help me in this...


回答1:


That overly general constraint exception is nasty, where's the InnerException after so many complaints?!

This template may help identify the problem row and column but a "Fill" version of the query function is needed. E.g. GetDistinct*() --> Fill*(). Then a table can be created and interrogated for the row's error text.

    SomeTable tTable = new SomeTable()
    try {
       // sorry, if you have a GetData, change to the fill version
         someTable.FillByActiveLogin(tTable, loginName);
    } catch (System.Data.ConstraintException constrExc) {             
            System.Data.DataRow[] rowsErr = tTable.GetErrors();
            for (int i = 0; i < rowsErr.Count(); i++)
                if (rowsErr[i].HasErrors)
                  Dbg.WriteLine(rowsErr[i].RowError);
    }

(Thanks Michael S for this hint whoever/wherever you are!)




回答2:


I got this error in a function from a DLL that uses a stored procedure. The procedure did not return all the fields in the table. One of the fields excluded was one that cannot be null. That apparently caused the constraint exception. When I changed the procedure and the DLL to include that field, the exception went away.




回答3:


After spending ages on this problem myself, I have resolved it modifying the query in the dataset to return a dummy value (that can be ignored) for each key field that is not required in the output.

So your query would become...

Select distinct TestType, 1 as ID, "Dummy" as TestName, "Dummy" as TestFees, "Dummy" as TestDate  
from LabTestTypes


来源:https://stackoverflow.com/questions/8448271/failed-to-enable-constraints-one-or-more-rows-contain-values-violating-non-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!