Click here to Skip to main content
15,899,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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

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:
C#
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:
C#
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);
 
Share this answer
 
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?
 
Share this answer
 

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