C# – Implicit conversion from data type nvarchar to varbinary(max) is not allowed

ctsql

I am getting the following error from the code below:

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

 protected void btnOKImageUpload_Click(object sender, EventArgs e)
 {
     try
    {
        string filePath = "";
        string fileName = "";

        int UserId = Convert.ToInt32(hdnUserId.Value);
        if (fileImage.HasFile)
        {
            if (CheckFileType(fileImage.FileName))
            {
                filePath = Server.MapPath(Application["UploadFolder"].ToString());
                if (UserId > -1)
                {
                    fileName = "Image_" + UserId.ToString() + Path.GetExtension(fileImage.FileName);
                }
                else
                {
                    fileName = Path.GetFileName(fileImage.FileName);
                }
                string virFileName = Application["UploadFolder"].ToString() + "/" + fileName;
                string tmpFileName = Path.Combine(filePath, fileName);
                fileImage.SaveAs(tmpFileName);
                SessionData.LocationFloorPlanFile = tmpFileName;

                DataAccess.SaveEmployeeImage(UserId, fileName);

                hdnImageFileName.Value = fileName;
                txtImageUpload.Text = virFileName;
                //btnFloorPlanView.HRef = hdnFloorPlan.Value;
                btnImageUpload.Disabled = true;
                btnImageDelete.Enabled = true;
                hdnPostbackAction.Value = "UPLOAD";
            }
        }
   }
     catch (Exception ex)
     {
         hdnErrMsg.Value = ex.Message;
         //"An error has occurred while processing your request. Please contact support for further assistance.";
     }  
}                                                                 
public static void SaveEmployeeImage(int userId, string imageFilePath)
{
    ArrayList paramaters = getParamArray();
    paramaters.Add(getParam("@userId", DbType.Int32, userId));
    paramaters.Add(getParam("@imageFilePath", DbType.AnsiString, imageFilePath));

    executeNonQuery("xp_SaveEmployeeImage", paramaters);
}

My procedure takes userId and image, to insert into the table.

What data type do I need to change?

Best Answer

Well, you're passing that image as an AnsiString datatype, which is where the problem is occurring.

I think you need DbType.Binary.

But then, your parameter name is imageFilePath, so presumably you should actually be giving it a path to a file as a string? This could mean your xp is actually wrong.

Related Topic