Getting a “Value for <column> in table <table> is DBNull” error

不问归期 提交于 2019-12-13 15:24:56

问题


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:

"The value for column 'VocNeedDesc' in table 'ASIVocationalNeeds' is DBNull."

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));

回答1:


DBNull check apply on cell value not for whole columns

So Try this

EmploymentDataSet.ASIVocationalNeeds.Rows[i]["VocNeedDesc"] == DBNull.Value ? string.Empty : EmploymentDataSet.ASIVocationalNeeds[i].VocNeedDesc));

if you are using typeDatset you can check null values as

((ASIVocationalNeedsRow)EmploymentDataSet.ASIVocationalNeeds.Rows[i]).isVocNeedDescnull() ? string.Empty : EmploymentDataSet.ASIVocationalNeeds[i].VocNeedDesc));



回答2:


Have you tried comparing it with DBNull.Value?

Code may look-like

EmploymentDataSet.ASIVocationalNeeds[i].VocNeedDesc == DBNull.Value ? string.Empty : EmploymentDataSet.ASIVocationalNeeds[i].VocNeedDesc));


来源:https://stackoverflow.com/questions/10196092/getting-a-value-for-column-in-table-table-is-dbnull-error

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