Insert Photo from Grdiview in sql server table RRS feed

  • Question

  • User1623409651 posted

    Hi all,

    I have bind the images from folder path inside the gridview control , now i want to insert selected photos from gridview in sql server table in binary format.

    Is it possible with the gridview or any other approach.

    Thanking you,

    Wednesday, January 10, 2018 8:41 AM

All replies

  • User1400794712 posted

    Hi Rameezwaheed,

    You can get the full url of the selected image from gridview, then using ReadBytes to convert the image to binary. I make a demo, you can refer to it.

        <asp:GridView ID="gdImage" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
                        <asp:CheckBox ID="chkRow" runat="server" />
                <asp:TemplateField HeaderText="Image" ItemStyle-Width="150">
                        <asp:Image ID="Image1" runat="server" ImageUrl="Image Full URL" />
        <asp:Button ID="btnGetSelected" runat="server" Text="Get selected records" OnClick="GetSelectedRecords" />
    protected void GetSelectedRecords(object sender, EventArgs e)
        foreach (GridViewRow row in gdImage.Rows)
            if (row.RowType == DataControlRowType.DataRow)
                CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
                if (chkRow.Checked)
                    Image image = (Image)row.Cells[1].FindControl("Image1");
                    string path = image.ImageUrl;
                    string filename = Path.GetFileName(path);
                    FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fs);
                    Byte[] bytes = br.ReadBytes((Int32)fs.Length); //Get the Binary of the image.
                    using (var db=new TestContext())
                        Images imageStore = new Images
                            FileName = filename,
                            FileBinary = bytes

    Best Regards,


    Thursday, January 11, 2018 9:16 AM
  • User426001450 posted

    It is much better to save all images in a file and only save a reference URL in the Database. It is faster, more convenience and you don't load your database with unnecessary stuff that take much memory and may slow down the process. 

    Good luck !

    Saturday, January 13, 2018 5:34 AM