10 Years
Discussion Span
Last Post by postmaster

u want to store the images in the database

if that wht u want to do, i dont suggest that

instead i will tell u to store the file path in a table in the database and then when u want to view the image u select its path from the database.

that wht i will do


Image data is stored on a SQL database as binary data, and must be read as such. In order to create this DataGrid, you will need the page that it is on, say Default.aspx and another page to gather and display the image, say IMG.aspx. First look at IMG.aspx:

We need to read the binary data and output it within the page. We also need something to tell us what the id of the picture is in our database table. For this we will create a query in our URL. So the url of each image will be .../IMG.aspx?ID=XX, XX being the images ID. Then, in the code for that page, we gather the query value using Dim ImageID As Integer = Request.QueryString("ID") Then you need to set up a SQL Connection, Command, and DataReader, read the binary data and then write it to the page:

Dim iConn As New Data.SqlClient.SqlConnection("-Connection String Here-")
    Dim iCmd As New Data.SqlClient.SqlCommand("SELECT image FROM IMAGES WHERE image_id = " & ImageID, iConn)
    Dim iRead As Data.SqlClient.SqlDataReader

    iRead = iCmd.ExecuteReader()

Then, on Default.aspx, you add the datagrid and, instead of reading the binary data for the image from the database, you read the images ID, and add that to the url for the IMG.aspx page, which you then databind to an image or imagebutton's imageurl property.

This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.