简体   繁体   English

VB.Net中强类型数据集中的错误

[英]Error in strongly typed dataset in VB.Net

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 ) 我正在为我的项目使用Strongly Typed Dataset ,该Strongly Typed Dataset是作为DAL( Data Access Layer )的dll创建的

I have added the Sql Server Table into this dataset using the designer and has created a DataAdapter 我已使用设计器将Sql Server表添加到此数据集中,并创建了一个DataAdapter

这就是数据表的外观

It works fine when i insert using DataTableAdapter 当我使用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. 但是当我想在组合框或gridview中显示表中的数据时,出现此错误。

在Visual Studio中显示的错误

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... 我告诉我发现了问题所在,我只是使用DataSet设计器预览了数据,发现Function返回了这样的数据...

我预览的数据

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.. 因此,这应该仅返回一列,但数据集将返回5列,而其他列将返回null,并且TestName列是主列,返回时不应为null,因此存在问题。

To resolve this i tried to change the NullValue & AllowDBNull property to [Empty] and true respectively but that didn't worked for me. 为了解决这个问题,我尝试将NullValueAllowDBNull属性分别更改为[Empty]true但这对我没有用。

TestName is key so it cannot be Null. TestName是键,因此不能为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. 使用DataSet可以将TestName列设置为Nullable,但是当您尝试填充它时,即使您说AllowDBNull = True,也要引发ConstraintException。

My suggestion is to change Key field in your strong typed DataSet or to return an empty string inside TestName field. 我的建议是更改强类型数据集中的Key字段,或在TestName字段内返回一个空字符串。 Better the first approach, maybe set Id as keyfield. 更好的第一种方法,也许将Id设置为关键字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM