locked
How do you determine if a varbinary field is empty? RRS feed

  • Question

  • User-1072848465 posted

    Depending if the field "dataFile" has a value I want to display a string.

    <%# (Eval("dataFile") != System.DBNull.Value) ? String.Format("document not available") : String.Format("<a href='/viewFile.ashx?Id={0}' Target='_blank' ><span>View PDF</span></a>", Eval("Id"))%>

    Currently, records with no value are showing up with a value. Is System.DBNull.Value correct?

    Any help is much appreciated.

    Brad

    Thursday, April 23, 2015 4:06 PM

Answers

  • User281315223 posted

    A varbinary object translates to a byte[] with C#, so I suppose that you could check if it is not null and has a length greater than 0 after casting it as the appropriate type :

    <%# (Eval("dataFile") != System.DBNull.Value && (Eval("dataFile") as byte[]) != null && (Eval("dataFile") as byte[]).Length <= 0) ? String.Format("document not available") : String.Format("<a href='/viewFile.ashx?Id={0}' Target='_blank' ><span>View PDF</span></a>", Eval("Id"))%>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 23, 2015 4:22 PM

All replies

  • User281315223 posted

    A varbinary object translates to a byte[] with C#, so I suppose that you could check if it is not null and has a length greater than 0 after casting it as the appropriate type :

    <%# (Eval("dataFile") != System.DBNull.Value && (Eval("dataFile") as byte[]) != null && (Eval("dataFile") as byte[]).Length <= 0) ? String.Format("document not available") : String.Format("<a href='/viewFile.ashx?Id={0}' Target='_blank' ><span>View PDF</span></a>", Eval("Id"))%>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 23, 2015 4:22 PM
  • User-1716253493 posted

    You can modify your query like this

    SELECT DATALENGTH(dataFile) as [FileLength], ...
    <%# Convert.ToInt32(Eval("FileLength")) > 0 ? ... %>

    I have not test it yet, i'm not sure about null value

    If you get problem with null, modify the query like this

    SELECT ISNULL(DATALENGTH(dataFile),0) as [FileLength], ...

    Thursday, April 23, 2015 9:24 PM
  • User-1072848465 posted

    Thanks Rion, your solution worked for me!

    Friday, April 24, 2015 7:44 AM