locked
Images in reports RRS feed

  • Question

  • User190487818 posted

    I am trying to create a table with both text and images. Currently in my database I have the location of these images stored as text. So far I have been able to create a table similar to below, with the second column acting as a hyperlink to the relevent photo.

    Photo Name  Link to Photo

    Photo1          C:\Users\user1\Pictures\pic1

    Photo2          C:\Users\user1\Pictures\pic2

    I however want to be able to have the image itself coming up in the table. I have found web pages with instructions on how to add embedded images from a database into a report. THis is of help to me however as i only have a link to the photo saved in the database.

    Thanks in advance for any help.

    Tuesday, July 1, 2014 9:52 AM

Answers

  • User-166373564 posted

    HI,

    Welcome to asp.net forum.

    According to your description above, if you want to store an image into SQL Server, you need to read an image file into a byte array. Once you have image data in a byte array, you can easily store this image data in SQL Server using SQL parameters.

    //Read Image Bytes into a byte array
              byte[] imageData = ReadFile(txtImagePath.Text);

    To read images from SQL Server, prepare a dataset first which will hold data from SQL Server table. Bind this dataset with a gridview control on form.

    void GetImagesFromDatabase()
    {
        try
        {
            //Initialize SQL Server connection.
    
            SqlConnection CN = new SqlConnection(txtConnectionString.Text);
    
            //Initialize SQL adapter.
            SqlDataAdapter ADAP = new SqlDataAdapter("Select * from ImagesStore", CN);
    
            //Initialize Dataset.
            DataSet DS = new DataSet();
    
            //Fill datasetwith ImagesStore table.
            ADAP.Fill(DS, "ImagesStore");
    
            //Fill Grid with dataset.
            dataGridView1.DataSource = DS.Tables["ImagesStore"];
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    Detailed information: https://workspaces.codeproject.com/shabdar-ghata/store-or-save-images-in-sql-server 

    Regards

    Angie 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 3, 2014 10:23 PM