Click here to Skip to main content
14,826,451 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, can someone assist me I am working on the asp.net core web site where the users upload documents and image in one record. Below is the action I am using to create the record there are these two object of IFormfile interface Data and Img.
Data is for document and Img is for image
My code is working fine when both variable has values, my challenge is when one of these object is empty my code throw an exception error an object cannot be empty.
My question is:
Is there any way to assign a default value to the object of IFORMFILE interface so that a default value can be inserted in a database?

Below is the action and model I am using to create the record in database

What I have tried:

C#
public async Task<IActionResult> Create(IFormFile Data, IFormFile Img, UploadDocument tbl)
{
    if (ModelState.IsValid)
    {
        var msData = new MemoryStream();
        var msImg = new MemoryStream();

        Data.CopyTo(msData);
        Img.CopyTo(msImg);
        _context.Add(new UploadDocument
        {
            Name = Path.GetFileName(Data.FileName),
            imgName = Path.GetFileName(Img.FileName),
            ContentType = Data.ContentType,
            imgContentType = Img.ContentType,

            Data = msData.ToArray(),
            Img = msImg.ToArray(),
            Content = tbl.Content,
            Title = tbl.Title,
            DateOfTraining = tbl.DateOfTraining,
            CreatedOn = DateTime.UtcNow

        });

        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }
    return View();
}
Model
C#
public class UploadDocument
{
    public int id { get; set; }
    public string Title { get; set; }
    [AllowHtml]
    public string Content { get; set; }

    [Display(Name = "Training Date")]
    public string DateOfTraining { get; set; }
    public string Name { get; set; }
    public string imgName { get; set; }

    [DisplayName("Upload Image")]
    
    public byte[] Img { get; set; }
    public string ContentType { get; set; }
    public string imgContentType { get; set; }
    [DisplayName("Upload Document")]
    public byte[] Data { get; set; }
    public DateTime CreatedOn { get; set; }
}
Posted
Updated 29-Nov-20 22:27pm
v2

1 solution

The field already has a default value: null.

You're getting a NullReferenceException because you are trying to call a method on a reference which is null. You simply need to test for null before trying to call methods on the instance.
C#
public async Task<IActionResult> Create(IFormFile Data, IFormFile Img, UploadDocument tbl)
{
    if (!ModelState.IsValid)
    {
        return View();
    }
    
    var document = new UploadDocument
    {
        Content = tbl.Content,
        Title = tbl.Title,
        DateOfTraining = tbl.DateOfTraining,
        CreatedOn = DateTime.UtcNow
    };
    
    if (Data != null)
    {
        document.Name = Path.GetFileName(Data.FileName);
        document.ContentType = Data.ContentType;
        
        using var ms = new MemoryStream();
        Data.CopyTo(ms);
        document.Data = ms.ToArray();
    }
    
    if (Img != null)
    {
        document.imgName = Path.GetFileName(Img.FileName);
        document.imgContentType = Img.ContentType;
        
        using var ms = new MemoryStream();
        Img.CopyTo(ms);
        document.Img = ms.ToArray();
    }
    
    _context.Add(document);
    await _context.SaveChangesAsync();
    return RedirectToAction(nameof(Index));
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900