HEXAQ : A friendly question - answer site for IT professionals

Asp.NET SQL Server : How to Convert byte[] to varbinary?


Hello Friends,

I want to upload and store files like image,.doc,.pdf,.xls etc in Sql Server database. In the database, I have created a field of datatype varbinary. From the Asp.net page, the file is uploaded and converted to byte[]. Files can be more that 128 MB(*). But while inserting, I am facing some issues.

code abstract public Int32 SaveFileAttachment(Int32 FA_ID, Byte[] FA_Data) { Int32 Success = 0; Database db = DatabaseFactory.CreateDatabase(); String sqlCommand = "uspSaveFileAttachment"; DbCommand dbCommandWrapper = db.GetStoredProcCommand(sqlCommand); //

    db.AddInParameter(dbCommandWrapper, "@FA_ID",DbType.Int32, FA_ID);

    db.AddInParameter(dbCommandWrapper, "@FA_Data", DbType.Binary, FA_Data);

    db.AddInParameter(dbCommandWrapper, "@out_FA_ID", DbType.Int32);
    dbCommandWrapper.Parameters["@out_FA_ID"].Direction = ParameterDirection.Output;
    Success = Convert.ToInt32(dbCommandWrapper.Parameters["@out_FA_ID"].Value.ToString());
    return Success;


Error message I get on executing above code:

Error Cannot implicitly convert type 'ERPC.Data.FileAttachmentData' 
to 'ERPC.Provider.IFileAttachmentProvider'. 
An explicit conversion exists (are you missing a cast?

Then I tried to change DbType to SqlDbType.varbinar.But it is also showing error

Error Argument 3: cannot convert from 'System.Data.SqlDbType' to 'System.Data.DbType'

what I should do with this "DbType.Binary"?

Please help me out


≡ Total 2 Answers


Try this way in case you wanna store uploaded PDF file:

int ilenght = FileUpload1.PostedFile.ContentLength;byte[] data = new byte[ilenght];// convert uploaded file in byteFileUpload1.PostedFile.InputStream.Read(data, 0, ilenght);using (SqlCommand cmd = new SqlCommand("insert into tblData " + "(PdfData) values(@data)", conn)){   cmd.Parameters.Add("@data", data);   cmd.ExecuteNonQuery();}

Please make your related changes for connection and stored procedure.

Hope this helps



Do you want something like this?

protected void btnUploadImage_Click(object sender, EventArgs e)        {            string ImageName = string.Empty;            byte[] Image = null;            if (ImageUploadToDB.PostedFile != null && ImageUploadToDB.PostedFile.FileName != "")            {                ImageName = Path.GetFileName(ImageUploadToDB.FileName);                Image = new byte[ImageUploadToDB.PostedFile.ContentLength];                HttpPostedFile UploadedImage = ImageUploadToDB.PostedFile;                UploadedImage.InputStream.Read(Image, 0, 


            }            using (SqlConnection Sqlcon = new SqlConnection(strCon))            {                using (SqlCommand cmd = new SqlCommand())                {                    Sqlcon.Open();                    cmd.Connection = Sqlcon;                    cmd.CommandType = CommandType.StoredProcedure;                    cmd.CommandText = "SP_ImageUpload";                    cmd.Parameters.Add(new SqlParameter("@pvchAction", SqlDbType.VarChar, 50));                    cmd.Parameters.Add(new SqlParameter("@pvchImageName", SqlDbType.VarChar, 100));                    cmd.Parameters.Add(new SqlParameter("@pvchImage", SqlDbType.Image));                    cmd.Parameters.Add(new SqlParameter("@pvchCreatedBy", SqlDbType.VarChar, 100));                    cmd.Parameters.Add("@pIntErrDescOut", SqlDbType.Int).Direction = ParameterDirection.Output;                    cmd.Parameters["@pvchAction"].Value     = "insert";                    cmd.Parameters["@pvchImageName"].Value  = ImageName;                    cmd.Parameters["@pvchImage"].Value      = Image;                    cmd.Parameters["@pvchCreatedBy"].Value  = "Admin";                    cmd.ExecuteNonQuery();                    int retVal = (int)cmd.Parameters["@pIntErrDescOut"].Value;                }            }            LoadImages();        }

Ref: http://www.dotnetgallery.com/kb/resource21-How-to-store-and-retrieve-images-from-SQL-server-database-using-aspnet.aspx


Enter your reply here
Answer Preview: