Verify and try to solve: The Gridview cannot directly bind images in the database (Note: Yes

Source: Internet
Author: User
I have read an article in the past, saying that the Gridview cannot directly bind images in the database. Please use HttpHandler ..., in the original article, we all know that the content in the Gridview cannot be straight ..., unfortunately, I did not know why I couldn't bind it. Today, I want to learn from it. This article briefly discusses the binding technology,

I have read an article in the past, saying that the Gridview cannot directly bind images in the database. Please use HttpHandler ..., in the original article, we all know that the content in the Gridview cannot be straight ..., unfortunately, I did not know why I couldn't bind it. Today, I want to learn from it. This article briefly discusses the binding technology,

I have read an article in the past, saying: "The Gridview cannot directly bind images in the database. Please use HttpHandler... in the original article, "We all know that it cannot be straight in the Gridview... ", Unfortunately, I did not know why I couldn't bind it. Today, I want to learn from it.

This article briefly discusses the binding technology,Not agreeSave the image to the database.

  

Well, at first, we need to upload images to the database page. By writing code step by step, I suddenly realized that it is really obvious that it cannot be directly bound.

The key code for uploading images is as follows:

FileStream fs = File. Open (FileUpload1.PostedFile. FileName, FileMode. Open );

Byte [] bt = newbyte [fs. Length];

Fs. Read (bt, 0, (int) fs. Length );
// Other code
Cmd. CommandText = "insert into MyImage (image) values (@ img )";
SqlParameter sp = new SqlParameter ("@ img", bt );
// Other codes are not available

Obviously, image files are convertedByte []And then saved to the database.IMAGE Type,

The "image" read by the Gridview from the database is actually byte [], andMy understanding is:,The Gridview cannot convert byte [] into an image.!

By the way, the test results in the design view are as follows:

At that time, I was surprised to think that the binding was successful.

However, when running in a browser, it is found that it cannot be bound:

========================================================== ============================

In fact, it is not difficult to read others' titles and think that they are complicated.

A new page is created. The page queries the IMAGE field in the database based on the parameters passed by QueryString, and then outputs the result using context. response. xx to close the job.

In this example

Context. Response. End ();

Wait and check the information to see what the code will do.

In my graduation project, the "Verification Code function" also used a similar solution, that is, to create a page to generate a verification code in the image format. At that time, I used context. response. write output.

The Write method has three overload values: 1 is char, 2 is object, and 3 is string.

Context. Response. BinaryWrite has only one parameter, which is byte [],

This method is exactly the same as the byte [] Here.

========================================================== ==============================

In the gridview, create an ImageField with the binding address:

~ /CreateIMG. aspx? Id = {0}

The bound parameter is id.

This completes the so-called "using HttpHandler binding", mainly because I used this method before, so it is easy to understand.

However, I still really don't understand HttpHandler. I still need to study it carefully.

There are so many things to learn, just take a look.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.