Click here to Skip to main content
15,913,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am creating first one data table to save images in datagridview.
C#
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";
            dt.Rows.Add(drOLD);
        }

        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";
        dt.Rows.Add(dr);

        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

C#
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("<Row>");
                    xmlClassMaster.Append("<Name>" + dgvDemo.Rows[k].Cells[0].Value + "</Name>");
                    xmlClassMaster.Append("<GridImage><cdata>" + ((byte[])dgvDemo.CurrentRow.Cells[3].Value) + "</cdata></GridImage>");
                    xmlClassMaster.Append("</Row>");
                }

                if (xmlClassMaster.Length > 0)
                {
                    xmlClassMaster.Append("</ImageInGrid>");
                    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


C#
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
Posted

1 solution

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900