Hi, I was just wondering if anybody new of a way to store an encrypted file to an sql server database through a stored procedure. I have managed to encrypt a file selected from a FileUpload control but at the minute the encrypted file and the key are created and saved locally to the machine.

What I would like to be able to achieve is: under the one submit button have the file encrypted and then store the encrypted file and key to the database or force user A to browse for these files before submitting.

However, on execution I am getting an error stating that Input string was not in correct format and it is bugging out at the following line:

submitCommand.ExecuteReader();

Below is a section of the code that I am using to try to save the encrypted file and key to the database (as varbinary)

byte[] blobEcryptedFile = FileUpload_EncryptedFile.FileBytes;
            blobFile = blobEcryptedFile;

            byte[] blobPKey = FileUpload_Key.FileBytes;
            blobKey = blobPKey;

//Actual filename of the file
            string filename = FileUpload_submitAss.FileName;

submitCommand.Parameters.Add(new SqlParameter("@assignment", SqlDbType.VarBinary, blobFile.Length, ParameterDirection.Input, false, 0, 0, "Assignment", DataRowVersion.Current, blobFile));
            submitCommand.Parameters["@assignment"].Value = blobFile;  //The path to the encrypted file

            submitCommand.Parameters.Add(new SqlParameter("@filename", SqlDbType.NVarChar, 50, "Filename"));
            submitCommand.Parameters["@filename"].Value = filename;

            submitCommand.Parameters.Add(new SqlParameter("@publicKey", SqlDbType.VarBinary, blobKey.Length, ParameterDirection.Input, false, 0, 0, "pKey", DataRowVersion.Current, blobKey));
            submitCommand.Parameters["@publicKey"].Value = blobKey;    //The path to the key

submitCommand.ExecuteReader();
            submitCommand.Dispose();
            myConnection.Close();

            lbl_validation_submitAssignment.Text = "Your file has successfully been uploaded!";

Has anybody any ideas??

Error : Input String was not in correct format.

Try this :

1. First set the parameter name and sql data type for sqlcommand.
2. Set value for the parameter name.

Ex:

sqlcommand.Parameters.Add("@Name",sqlDbType.varchar)
sqlcommand.Parameters("@Name").value = value

:) Hope this will resolve your issue.

Share the knowledge and increase it.... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.