Click here to Skip to main content
14,664,400 members
Rate this:
Please Sign up or sign in to vote.
See more:
protected void btnSubmit_Click(object sender, EventArgs e)
    {

        if (fpPhoto.HasFile)
        {
            if (fpPhoto.PostedFile.ContentType == "image/jpg" || fpPhoto.PostedFile.ContentType == "image/jpeg" || fpPhoto.PostedFile.ContentType == "image/png")
            {
                byte[] imagebytes = new byte[fpPhoto.PostedFile.ContentLength];
               int filelenght = fpPhoto.PostedFile.ContentLength;
               imagebytes = fpPhoto.FileBytes;
               fpPhoto.PostedFile.InputStream.Read(imagebytes, 0, filelenght);
            }
        }

        User objUser = new User();

            objUser.UserName_Pk = txtUserName.Text;
            objUser.Password = txtPassword.Text;
            objUser.MobileNo = txtMobileNo.Text;
            objUser.Email = txtEmail.Text;
            objUser.SecurityAnswer = txtAnswer.Text;
            objUser.Photo = Convert.ToByte(imagebytes);//Here is the problem

        objUserBll.InsertUpdate(objUser);
Posted

Rate this:
Please Sign up or sign in to vote.

Solution 2

If you are trying to convert it to an image - and I can't think of anything else you'd want to convert it to - then try this:
MemoryStream ms = new MemoryStream(imagebytes);
Image returnImage = Image.FromStream(ms);

But...you need to move your definitions about anyway: imagesbytes is out of scope where you are trying to use it.
Try this:
Image userImage = null;
if (fpPhoto.HasFile)
{
    if (fpPhoto.PostedFile.ContentType == "image/jpg" || 
        fpPhoto.PostedFile.ContentType == "image/jpeg" || 
        fpPhoto.PostedFile.ContentType == "image/png")
    {
       userImage = Image.FromStream(fpPhoto.PostedFile.InputStream);
    }
}

User objUser = new User();

objUser.UserName_Pk = txtUserName.Text;
objUser.Password = txtPassword.Text;
objUser.MobileNo = txtMobileNo.Text;
objUser.Email = txtEmail.Text;
objUser.SecurityAnswer = txtAnswer.Text;
objUser.Photo = userImage;

objUserBll.InsertUpdate(objUser);
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

The problem is that imagebytes is a byte array.

As such you will never be able to convert it into a single byte, which is what Convert.ToByte() does.

What exactly is this code supposed to do? What sort of data type is User.Photo?
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100