0

I asked too many questions today but I have one more question. I'm trying to save an image into my database. I couldn't solve something. When I try to add image, it says paramater string can not be transleted into BYTE[]. Actually I'm giving bytes[] as paramater value.I tried to solve, but I couldn't find any answer maybe you can help me. Here is my code:

  Stream fs = FileUpload1.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(fs);
        Byte[] bytes = br.ReadBytes((Int32)fs.Length);

        //insert the file into database
        string strQuery = "INSERT INTO Books(Book_Name, Author_Name, Image,In_Lib) VALUES (@BN, @AN, @IM,@LIB)";
        SqlCommand cmd = new SqlCommand(strQuery);
        string val1 = "" + TextBox1.Text;
        string val2 = "" + TextBox2.Text;
        cmd.Parameters.Add("@BN", SqlDbType.VarChar).Value = val1;
        cmd.Parameters.Add("@AN", SqlDbType.VarChar).Value= val2;
        cmd.Parameters.Add("@IM", SqlDbType.Binary).Value = bytes;
        cmd.Parameters.Add("@LIB", SqlDbType.Binary).Value = "NO";
        InsertUpdateData(cmd);
        lblMessage.ForeColor = System.Drawing.Color.Green;
        lblMessage.Text = "File Uploaded Successfully";
1
  • 2
    may be the problem is here: cmd.Parameters.Add("@LIB", SqlDbType.Binary).Value = "NO"; Commented May 9, 2012 at 15:19

2 Answers 2

4

This line is invalid:

cmd.Parameters.Add("@LIB", SqlDbType.Binary).Value = "NO";

Based on your SQL, it looks like you intended to use a varchar there.

Sign up to request clarification or add additional context in comments.

Comments

3

You have passed the string "NO" as a value of the Binary parameter @LIB:

cmd.Parameters.Add("@LIB", SqlDbType.Binary).Value = "NO";

I guess that is your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.