954,190 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

SQL Server, Images and DataGrid in ASP.NET

How to upload Images to SQL Server database using ASP.NET tools.
Also how to retrieve those saved images from SQL Server and display them onto DataGrid.
Visit: http://www.programmingknowledge.com/ImageUpload.aspx

postmaster
Newbie Poster
19 posts since May 2006
Reputation Points: 10
Solved Threads: 1
 

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

web_developer
Junior Poster in Training
84 posts since Mar 2006
Reputation Points: 10
Solved Threads: 1
 

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

    iConn.Open()
    iRead = iCmd.ExecuteReader()
    Response.BinaryWrite(iRead("image"))
    iConn.Close()


Then, onDefault.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.

ChimpusDupus
Light Poster
47 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 
postmaster
Newbie Poster
19 posts since May 2006
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You