I 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(Data Access Layer
)
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.
TestName is key so it cannot be Null. The DataSet let you set TestName column as Nullable, but when you try to fill it, raise ConstraintException, even if you said AllowDBNull = True.
My suggestion is to change Key field in your strong typed DataSet or to return an empty string inside TestName field. Better the first approach, maybe set Id as keyfield.