Ouss Ama
Ouss Ama

Reputation: 123

How can i make nulleable Byte[]

Hi everyone I can't take a null value for my image property I have this error:

Implicit conversion from data type nvarchar to varbinary(max) is not allowed. use the convert function to run this query.

This is my prop class:

namespace testhopital
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;

    public partial class UserProfile
    {
        public UserProfile()
        {
            this.webpages_Roles = new HashSet<webpages_Roles>();
        }

        public int UserId { get; set; }
        [Display(Name = "User name")]
        public string UserName { get; set; }
        [Required]
        [Display(Name = "Prenom")]
        public string UserSurname { get; set; }
         [Display(Name = "Email")]
        public string UserEmail { get; set; }
        [Display(Name = "Date Naissance")]
        [DataType(DataType.Date)]
        public Nullable<System.DateTime> UserBirthday { get; set; }
        [Column(TypeName = "image")]
        [Display(Name = "Votre Photo")]
        public byte[] UserPicture { get; set; }
         [Display(Name = "Adresse")]
        public string UserAdress { get; set; }
        [Display(Name = "numero de tlephone")]
        public string UserPhone { get; set; }
          [Display(Name = "Description")]
        public string UserDescription { get; set; }
        [Display(Name = "Date de creation")]
        public Nullable<System.DateTime> UserCreateAcount { get; set; }

        [Display(Name = "Date de modification")]
        public Nullable<System.DateTime> UserLastUpdate { get; set; }
        public Nullable<int> RoleId { get; set; }
        [Required]
        [Display(Name = "User password")]
        [DataType(DataType.Password)]
        public string password { get; set; }

        [Required]
        [Display(Name = "Confirm password")]
        [Compare("password", ErrorMessage = "mot de passe pas valider")]
        [DataType(DataType.Password)]
        public string confirmPassword { get; set; }

        public virtual ICollection<webpages_Roles> webpages_Roles { get; set; }
        public virtual webpages_Roles webpages_Roles1 { get; set; }
    }
}

and my controller:

  [HttpPost]
    public ActionResult Register(UserProfile register, HttpPostedFileBase image2)
    {


        if (ModelState.IsValid)
        {

            try
            {
                register.UserPicture = new byte[image2.ContentLength];
                image2.InputStream.Read(register.UserPicture, 0, image2.ContentLength);

            }
            catch (Exception)
            {

                register.UserPicture = null;
            }

            WebSecurity.CreateUserAndAccount(register.UserName, register.password, new
                          {

                              UserSurname = register.UserSurname,
                              UserEmail = register.UserEmail,
                              UserBirthday = register.UserBirthday,

                              UserPicture = register.UserPicture,
                              UserAdress = register.UserAdress,
                              UserPhone = register.UserPhone,
                              UserDescription = register.UserDescription,
                              UserCreateAcount = DateTime.Now,


                          },true);

            return RedirectToAction("Login", "AccountHopital");
        }
        return View(register);

    }

I have error if I make a null Image. I need to make image to null value not a byte[0]

Upvotes: 2

Views: 6500

Answers (1)

detarallt
detarallt

Reputation: 313

Arrays, including byte arrays, are nullable by default. If you initialize the byte array in this way, an exception will throw when image2 is null:

register.UserPicture = new byte[image2.ContentLength];

The exception will throw because image2 is null, and it therefore does not have a "ContentLength" property to get. Instead, this should work:

if(image2 != null) 
{
   register.UserPicture = new byte[image2.ContentLength];
   image2.InputStream.Read(register.UserPicture, 0, image2.ContentLength);
}

That way, you'll only initialize a byte array if you have an image.

Edit: I read too quickly. I see that you're using exception handling to catch this, which is not a good practice. Exceptions should only be used for true exception cases.

The real issue here most likely has to do with the dynamic handling of a byte[] parameter passed to the WebSecurity.CreateUserAndAccount() function. You may want to convert the byte array to a base 64 string: Convert.ToBase64String(register.UserPicture), when creating the user. When it's time to read the image from the database, you can use Convert.FromBase64String(model.UserPicture) to retrieve the byte array from the base 64 string.

Upvotes: 4

Related Questions