We help IT Professionals succeed at work.

What's wrong with this SQL statement?

rmmarsh
rmmarsh asked
on
752 Views
Last Modified: 2012-08-14
I am trying to add an image to a table... to do that, I understand I need a TEXTPTR, which is what I'm trying to get here...
I have the following code:

            //  now find the record in the table
            Int32 textPtr = 0;
            string findTextPtr = "SELECT @textPtr = TEXTPTR(BookImage) FROM tBooks WHERE BookNbr = '" + tbBookNbr.Text + "'";
            SqlCommand cmd = new SqlCommand(findTextPtr, bookConn);
            SqlParameter parm = new SqlParameter("@textPtr", SqlDbType.Image, 16, "BookImage");
            cmd.Parameters.Add(parm);
            cmd.ExecuteReader();


for which I get the following error:

Parameterized Query '(@textPtr image)SELECT @textPtr = TEXTPTR(BookImage) FROM tBooks' expects parameter @textPtr, which was not supplied.

How do I fix this?
Comment
Watch Question

Daniel ReynoldsSoftware Applications Developer / Integrator
CERTIFIED EXPERT

Commented:
If you are attempting to insert a record into the table, why are you performing a SELECT?

Author

Commented:
Here is the code for the insert (actually a WRITETEXT)... I need to get the TEXTPTR first, right?

            cmd = new SqlCommand("WRITETEXT tBooks.BookImage @textPtr @imageData", bookConn);
            cmd.Parameters.Add("textPtr", SqlDbType.Binary, 16);
            cmd.Parameters.Add("imageData", SqlDbType.Image);
            cmd.Parameters["textPtr"].Value = textPtr;
            cmd.Parameters["imageData"].Value = imageData;

            SqlParameter param1 = new SqlParameter("@imageData", SqlDbType.Image);
            param1.Value = imageData;
            cmd.Parameters.Add(param1);

            cmd.ExecuteNonQuery();

Author

Commented:
I found the answer... thanks anyway...

            //  now find the record in the table
            Int32 textPtr = 0;
            string findTextPtr = "SELECT @textPtr = TEXTPTR(BookImage) FROM tBooks WHERE BookNbr = '" + tbBookNbr.Text + "'";
            SqlCommand cmd = new SqlCommand(findTextPtr, bookConn);
            SqlParameter parm = cmd.Parameters.Add("@textPtr", SqlDbType.VarBinary, 100);
            parm.Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();

Author

Commented:
OK... so, I appear to have successfully gotten the TEXTPTR, but now the WRITETEXT is failing... here's my set of code...

            // Access the file stream; store the file in a memory byte array.
            FileStream streamBinary = new FileStream(sFilename, FileMode.Open);
            BinaryReader readerInput = new BinaryReader(streamBinary);

            //  get length of file
            FileInfo fi = new FileInfo(sFilename);
            int fileSize = (int)fi.Length;

            //  read it into a byte array
            byte[] imageData = readerInput.ReadBytes(fileSize);
            streamBinary.Close();
            readerInput.Close();

            //  now find the record in the table
            Int32 textPtr = 0;
            string findTextPtr = "SELECT @textPtr = TEXTPTR(BookImage) FROM tBooks WHERE BookNbr = '" + tbBookNbr.Text + "'";
            SqlCommand cmd = new SqlCommand(findTextPtr, bookConn);
            SqlParameter parm = cmd.Parameters.Add("@textPtr", SqlDbType.VarBinary, 100);
            parm.Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();

            //  now, write the image to the record
            cmd = new SqlCommand("WRITETEXT tBooks.BookImage @textPtr @imageData", bookConn);
            SqlParameter p1 = cmd.Parameters.Add("@textPtr", SqlDbType.VarBinary, 100);
            p1.Direction = ParameterDirection.Input;
            SqlParameter p2 = cmd.Parameters.Add("@imageData", SqlDbType.Image);
            p2.Direction = ParameterDirection.Input;

            cmd.Parameters["@textPtr"].Value = textPtr;
            cmd.Parameters["@imageData"].Value = imageData;
            cmd.ExecuteNonQuery();

Now, when I execute this, I get the following error:

Failed to convert parameter value from a Int32 to a Byte[].

What parameter is it talking about?
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks for your help... I found that I don't need the TEXTPTR if I use varbinary(max) to store the image.  Works like a champ!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.