My goal is to execute a linq to sql query that will return a value from a varbinary(max) database field if the varbinary(max) field is not null. In the code below, x.doc is a varbinary(max) in the database.
The basics of my code is this:
var pdfquery = from x in dataContext.Statements
where x.enccc == card && x.stDate == datetime
select x.doc;
if (pdfquery.Count() == 1 )
{
pdffile = pdfquery.FirstOrDefault().ToArray();
}
else
{
//go to a webservice to get pdffile and write it to the db
}
The code block returned a null value because the if statement was true. There is a null value in the database when the two parameters are passed.
Where the section of "pdfquery.Count() == 1" is, I've tried:
All of those are giving me a Null Value Exception.
What am I missing? How do I identify when a query that returns a null value varbinary(max) so that I can take appropriate action?
Update (07/17/14): I decided to handle the Null Refernece Exception with a try catch:
try
{
var pdfquery = from x in dataContext.Statements
where x.enccc == card && x.stDate == datetime
select x.doc;
pdffile = pdfquery.SingleOrDefault().ToArray(); //gets the binary data and converts it to a byte array
}
catch(NullReferenceException nux)
{
logger.LogDebug("No Binary Data Exists for Statement, making Request ---- ",nux);
getStatment(unencArray);
getByteArray(statementxml);
writeByteArrayToDb(unencArray, pdffile);
}
I don't really like doing this, because I'd rather be able to catch other exceptions if they're thrown. I'm going to try changing my Byte[] to ?Byte[] in hopes that the null value will be handled normally.