locked
in C#, How can i check image field is null or not? RRS feed

  • Question

  • User-1410946272 posted

    Actually i have a table with 7 columns

    1) Title  varchar(100)

    2)Description varchar(300)

    3)ProfilePicture varbinary(max)

    4) Image1 varbinary (max)

    5)Image2  varbinary(max)

    6)Image3 varbinary(max)

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

    if((byte[])ds.Tables[0].Rows[0]["Image3"] != null

    )

    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.

    Monday, March 28, 2011 2:52 PM

Answers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 28, 2011 3:28 PM
  • User-1501102275 posted

    Try this:

    if(ds.Tables[0].Rows[0]["Image3"] != DBNull.Value) ...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 28, 2011 3:31 PM
  • User3866881 posted

    HI:)

    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:

    Sample1:select null:It will return a DBNull.Value in C#. Because it will return a single row with the Null value.

    Sample2:select * from xxx where 1=0:This will return a common null value equaling to C#, because it will return no rows.

    As 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.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 30, 2011 10:26 PM

All replies

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 28, 2011 3:28 PM
  • User-1501102275 posted

    Try this:

    if(ds.Tables[0].Rows[0]["Image3"] != DBNull.Value) ...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 28, 2011 3:31 PM
  • User3866881 posted

    HI:)

    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:

    Sample1:select null:It will return a DBNull.Value in C#. Because it will return a single row with the Null value.

    Sample2:select * from xxx where 1=0:This will return a common null value equaling to C#, because it will return no rows.

    As 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.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 30, 2011 10:26 PM