Click here to Skip to main content
14,237,457 members
Rate this:
Please Sign up or sign in to vote.
i am creating first one data table to save images in datagridview.
DataTable dt = new DataTable();
        dt.Columns.Add("Name", typeof(string));//0
        dt.Columns.Add("Upload", typeof(string));//1
        dt.Columns.Add("Show", typeof(string));//2
        dt.Columns.Add("image", typeof(byte[]));//3

        for (int i = 0; i < dgvDemo.RowCount-1; i++)
            DataRow drOLD = dt.NewRow();
            drOLD["image"] =dgvDemo.Rows[i].Cells[3].Value;
            drOLD["Name"] = dgvDemo.Rows[i].Cells[0].Value;
            drOLD["Upload"] = "Upload";
            drOLD["Show"] = "Show";

        Image img = PBImage.Image; //Image.FromFile(@"physical path to the file");
        DataRow dr = dt.NewRow();
        dr["image"] = imageToByteArray(img);
        dr["Name"] = "Image";
        dr["Upload"] = "Upload";
        dr["Show"] = "Show";

        dgvDemo.DataSource = dt;

    public byte[] imageToByteArray(System.Drawing.Image imageIn)
        MemoryStream ms = new MemoryStream();
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        return ms.ToArray();

Following is code is use for save images in database using XML as parameter for Store Procedure

hash = new Hashtable();
            hash.Add("@QueryNo", QueryNo);
            string strXmlCategory_Section = "";
            StringBuilder xmlClassMaster = new StringBuilder();
              for (int k = 0; k < dgvDemo.Rows.Count-1; k++)
                    xmlClassMaster.Append("<Name>" + dgvDemo.Rows[k].Cells[0].Value + "</Name>");
                    xmlClassMaster.Append("<GridImage><cdata>" + ((byte[])dgvDemo.CurrentRow.Cells[3].Value) + "</cdata></GridImage>");

                if (xmlClassMaster.Length > 0)
                    strXmlCategory_Section = "<ImageInGrid>" + Convert.ToString(xmlClassMaster);
                hash.Add("@strImageInGrid", strXmlCategory_Section);

hash table is passing as parameter list to SP.

Store Procedure Code in Database

Exec sp_xml_prepareDocument @DocHandle_ImageInGrid output, @strImageInGrid

select Name, GridImage
        into #temp 
        from OPENXML (@DocHandle_ImageInGrid, '/ImageInGrid/Row',12)
        with (

                Name varchar(50) 'Name', 
                GridImage varbinary(max) 'GridImage'


    insert into dbo.GridImage
        ( Name, GridImage)
    Select  Name, GridImage
    From #temp

i Have Problem in Fetch Image from database & it to Grid . error is -parameter is not valid. at system.drawing.image.fromstream i think that my save method is wrong please help

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

seems you are right
try to use the Dataset.Getxml[^]
and send it to the Storedproc, also change the xpath as required

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100