Last post Mar 30, 2011 10:26 PM by Decker Dong - MSFT
Mar 28, 2011 02:52 PM|koundinya.sarma|LINK
Actually i have a table with 7 columns
1) Title varchar(100)
4) Image1 varbinary (max)
Image1,2,3,4 are not a mandatory fields so, when i inserted, i check it if it is not there then i insert null value,
Now problem is when i retrive them if there is a row contain image1,2 are image content and 3,4 are null values
dataset also filling as it is in database
but when i checking like
it shows error like
Unable to cast object of type 'System.DBNull' to type 'System.Byte'.
so, please tell me how to check if the field is null or not.
Mar 28, 2011 03:28 PM|raghav_khunger|LINK
Please check for same: http://forums.asp.net/t/1145956.aspx/1?Unable+to+cast+object+of+type+System+DBNull+to+type+System+Byte+
Mar 28, 2011 03:31 PM|jameswright|LINK
if(ds.Tables.Rows["Image3"] != DBNull.Value) ...
Mar 30, 2011 10:26 PM|Decker Dong - MSFT|LINK
To decide whether a column value is Null in SQL, you cannot use general null, because the "null" is quite different from "null" in sql db. In fact, all the columns values taken out of the sql,
if you want to check whether they are "null" or not, please use DBNull.Value instead of general "null"：）
However, if you cannot get any row from a sql's select statement, thus you can use null directly not DBNull.Value——Here're two samples:
null：It will return a DBNull.Value in C#. Because it will return a single row with the Null value.
* from xxx where 1=0：This will return a common null value equaling to C#, because it will return no rows.
to this, since your image rows can be fetched, however the value of the image is Null in SQL, you DBNull.Value is your choice.