2

I'm coding an Admin form. The user may or may not update PDF file(s); if he does I'm handling it in my form coded as follows:

string fileExt = System.IO.Path.GetExtension(regFormUpload.FileName);
if (regFormUpload.HasFile)
{
    if (string.Equals(fileExt, ".pdf", StringComparison.OrdinalIgnoreCase))
    {
        Stream pdfFileStream = regFormUpload.PostedFile.InputStream;
        fileData = new byte[regFormUpload.PostedFile.ContentLength];
        pdfFileStream.Read(fileData, 0, regFormUpload.PostedFile.ContentLength);

        // Call DALC method with uploaded file(s) params
    }
    else
    {
        regFormCustVal.IsValid = false;
    }
}
else
{
    // Call DALC method indicating "null" for non-uploaded files(s)???
}

Is there a way to pass a null to the DALC/Stored Procedure? It's being stored in SQL Server as a varbinary(max) type. Previously I used to code separate methods depending on whether or not someone uploaded a file and I know there has to be an easier way but the C# syntax is escaping me.

UPDATE - 05-24-2012

I still haven't got an answer to this. In my DALC, I currently have to call several DB update methods, depending on whether files (byte[]) has been uploaded or not. Is there anyway to get around this? I have two separate file upload controls and both are non-required fields. This means I have to code up to four separate DB update methods to account for the (2^2) possibilities!

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91

2 Answers2

1

There's no such thing as a Nullable Byte Array, you can always set an Array to null.

As for the file upload, you might do it like the documentation for FileUpload.PostedFile suggests in the example.

UPDATE: Not sure how your DALC works but for passing the array to an stored procedure just make sure to pass DBNull.Value when the array is null.

UPDATE 2: You have to pass DBNull.Value to the procedure, for the DAL just set the array to null.

fileData = null;

Then when you call the procedure you have to check the property and either pass the value or DBNull.Value. See this question on how to do that.

Community
  • 1
  • 1
shriek
  • 5,157
  • 2
  • 36
  • 42
0

The simple answer to this was to code four overrides for the DB Add method - which was what I was trying to avoid in the first place. Received no response showing a more elegant way to do this.

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91