Organizational Research By

Surprising Reserch Topic

asp net store image in sql and retrieve for aspimage

asp net store image in sql and retrieve for aspimage  using -',sql,image'

I am looking to fileupload a picture jpeg,gif,etc into an SQL database on an updateprofilepicture page. Then on the profile page, I want to retrieve the image from an sql database and have it show up in an Asp:Image control. I have much code trying to do this and it doesn't work. The table contains a column of type Image.

asked Oct 19, 2015 by nikhilapatil
0 votes

Related Hot Questions

6 Answers

0 votes

As Joel mentioned you should use an HttpHandler or a page to display the image. Here is a sample code to output image (Image.ashx) :

// ProcessRequest method of Image.ashx
long imageId = Convert.ToInt64(Request.QueryString["ImageId"]);

using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand(
    "SELECT ImageFile FROM ImageTable WHERE ImageId = @ImageID", conn))
    command.Parameters.Add("@ImageID", SqlDbType.Int).Value = imageId;

    Response.ContentType = "image/gif";
    Response.BinaryWrite((byte[]) command.ExecuteScalar());

and then use image in your page as :

answered Oct 19, 2015 by param.oncemore
0 votes

The important thing to remember here is that you shouldn't try to transmit the image data with the profile page itself. Instead, you want your profile page to generate HTML markup for the browser that looks something like this:

User 1234 avatar

That is the ultimate result of your control. Then the browser will send a completely separate Http request to retrieve the image. That's how pictures on web sites work. You then need to be able to handle that additional request. To do that, create an Http handler (*.ashx file) and use it to retrieve the appropriate image data from the database and send it to the browser.

answered Oct 19, 2015 by patilkiran.101
0 votes

If you're using SQL 2005 or greater you should not use the data type Image because it's now deprecated. Instead you want to use the new Varbinary(MAX) type if possible. Once you have it stored all you need to do is retrieve it via ADO.Net call and cast the cell value into type Byte[] and then call Response.BinaryWrite like in ScarletGarden's example above.

answered Oct 19, 2015 by abhimca2006
0 votes

After a few hundred gigabytes of images, I believe you'll find yourself thinking that the operating systems' file system and static file http servers is better suited than the database, which is busy which a lot of other details, for storing images. It also allows you to use thousands of existing free tools to work with, move, host, etc the images.

answered Oct 19, 2015 by sanjaypal1983
0 votes
protected void Page_Load(object sender, EventArgs e) {
    GridView1.DataSourceID = "";
    GridView1.DataSource = SqlDataSource1;

protected void btnSubmit_Click(object sender, EventArgs e) {
    string strImageName = txtImageName.Text.ToString();

    if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "") {
        byte[] imageSize = new byte[FileUpload1.PostedFile.ContentLength];
        HttpPostedFile uploadedImage = FileUpload1.PostedFile;
        uploadedImage.InputStream.Read(imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);

        // Create SQL Connection
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=RND3" + "\\" + "SQLEXPRESS;Initial Catalog=SSSolutionFiles;Integrated Security=True";

        // Create SQL Command 
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "INSERT INTO Imagess(ImageName,Image)" + " VALUES (@ImageName,@Image)";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;

        SqlParameter ImageName = new SqlParameter("@ImageName", SqlDbType.VarChar, 50);
        ImageName.Value = strImageName.ToString();

        SqlParameter UploadedImage = new SqlParameter("@Image", SqlDbType.Image, imageSize.Length);
        UploadedImage.Value = imageSize;

        Label1.Text = "File Uploaded";

        GridView1.DataSourceID = "";
        GridView1.DataSource = SqlDataSource1;
answered Oct 19, 2015 by rajeevr642
0 votes

Try these links it might help you..

you can also try by storing the image files on the server and store the paths on the Sql table.. by these links

answered Oct 19, 2015 by rajeshujade