jeudi 4 août 2016

How do I check if a file got uploaded to my page(if-loop)

My Webpage is built with Asp.Net, C# and Entity Framework. I created my Database with EF Code First:

public byte[] passfoto { get; set; }

of course i have many more but these are not relevant for my question. This is a image datatype. Here is how i created my fileupload with asp.net. I also added a label called LabelPassfotoError. I talk about that below. My AspCode

<tr>
    <td>
        <asp:Label ID="LabelPassfoto" runat="server" Text="Passfoto:">/asp:Label></td>
    <td>
        <asp:FileUpload ID="FileUploadPassfoto" runat="server" Style="background-color: #533473; border-color: lightgray; width: 100%;" /><asp:Label ID="LabelPassfotoError" ForeColor="Red" runat="server" Text="Ungültiges Passfoto" Visible="false"></asp:Label></td>
</tr>

Now i want to make a if-loop to check if something got uploaded to my page(FileUploadPassfoto tag). I've tried it like this:

if(FileUploadPassfoto.HasFile == true)
        {
            lehrling.passfoto = FileUploadPassfoto.PostedFile;
            LabelPassfotoError.Visible = false;

        }
        else
        {

            LabelPassfotoError.Visible = true;
        }

I get this error when i do it like i explained:

my error code

This is how it should work: If something got uploaded the Errorlabel is not visible and the file gets attatched to the lehrling.passfoto object to store it in my DB. If the file doesnt exist, the errorlabel gets set to Visible = true so the user knows that he forgot to upload.

How can i relize this?

Aucun commentaire:

Enregistrer un commentaire