Search code examples
.netwcfvisual-studio-2008

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


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

Solution

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