获得< column>的值在表< table>中是DBNull"错误 [英] Getting a "Value for <column> in table <table> is DBNull" error
问题描述
我正在使用WCF服务,在该服务中,我试图将传递给该方法的一个数据表中的数据加载到本地数据库中的另一个数据表中(在这种情况下,本地"数据库是Windows 2003 Server上的SQL Server数据库).当我收到以下错误消息:
I'm working on a WCF service, in which I'm trying to load data from one data table passed to the method, into another data table from the local database (in this case, the "local" database is a SQL Server database on a Windows 2003 Server). When I do I get the following error message:
表'ASIVocationalNeeds'中'VocNeedDesc'列的值为DBNull."
"The value for column 'VocNeedDesc' in table 'ASIVocationalNeeds' is DBNull."
这实际上是可以的,因为表ASIVocationalNeeds中的VocNeedDesc列允许为空.最初,我只是将传递的表中的VocNeedDesc的值分配给了本地表,但是却遇到了该错误.因此,接下来我更改代码以检查VocNeedDesc的值是否为空,如果是,我将string.Empty分配给该值,否则我分配了传递的值,但仍然收到错误消息.我很沮丧不知道为什么我会收到此错误.这是相关的代码段:
This actually is OK, because column VocNeedDesc in table ASIVocationalNeeds allows nulls. Originally I just assigned the value of VocNeedDesc from the passed table to the local table, but I got that error. So next I changed my code to check the value of VocNeedDesc to see if it was null, and if so I assigned string.Empty to the value, otherwise I assigned the passed value, but I still get the error message. I'm stumped; don't know why I'm getting this error. Here's the relevant code segment:
localEmploy.ASIVocationalNeeds.Rows.Add(clientNumber,
caseNumber,
0,
EmploymentDataSet.ASIVocationalNeeds[i].VocNeedType,
(EmploymentDataSet.ASIVocationalNeeds[i].VocNeedDesc == null ? string.Empty : EmploymentDataSet.ASIVocationalNeeds[i].VocNeedDesc));
推荐答案
DBNull检查不适用于整个单元格的值
DBNull check apply on cell value not for whole columns
所以尝试这个
EmploymentDataSet.ASIVocationalNeeds.Rows[i]["VocNeedDesc"] == DBNull.Value ? string.Empty : EmploymentDataSet.ASIVocationalNeeds[i].VocNeedDesc));
如果您使用的是typeDatset您可以将空值检查为
if you are using typeDatset you can check null values as
((ASIVocationalNeedsRow)EmploymentDataSet.ASIVocationalNeeds.Rows[i]).isVocNeedDescnull() ? string.Empty : EmploymentDataSet.ASIVocationalNeeds[i].VocNeedDesc));
这篇关于获得< column>的值在表< table>中是DBNull"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!