Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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));
like image 857
Rod Avatar asked Apr 17 '12 17:04

Rod


People also ask

What is DBNull error?

DBNull represents a nonexistent value returned from the database. In a database, for example, a column in a row of a table might not contain any data whatsoever. That is, the column is considered to not exist at all instead of merely not having a value.

What is DBNull value in SQL Server?

DBNull is a singleton class, which means only this instance of this class can exist. If a database field has missing data, you can use the DBNull. Value property to explicitly assign a DBNull object value to the field. However, most data providers do this automatically.

Is DBNull value the same as null?

Do not confuse the notion of null in an object-oriented programming language with a DBNull object. In an object-oriented programming language, null means the absence of a reference to an object. DBNull represents an uninitialized variant or nonexistent database column.

Does not allow DBNull value SQL?

The two possible reasons I know of for the does not allow DBNull error are: Columns are in the wrong order, which is solved by either putting them in the same order as their Database Ordinal, or by performing a Column Mapping. KeepNulls is enabled, and DBNull. Value (or null ?) are set in the DataTable.


1 Answers

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));
like image 142
Sadaf Avatar answered Nov 15 '22 05:11

Sadaf