inserting imagecolumn image to sql server

5 posts, 0 answers
  1. ahmed
    ahmed avatar
    14 posts
    Member since:
    Feb 2018

    Posted 07 Nov 2020 Link to this post

    i have radgridview that contains 2 text columns and the third is an imagecolumn 

     

    i need to insert the data in the gridview into sql server table including the image in column3

     

    how to do that please

  2. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    4131 posts

    Posted 09 Nov 2020 Link to this post

    Hello, Ahmed, 

    I would recommend you to bind your RadGridView to the data table that you have in the SQL server: https://docs.telerik.com/devtools/winforms/controls/gridview/populating-with-data/tutorial-binding-to-datatable-or-dataset 

    For this purpose you can use the RadGridView's "Data Source Configuration Wizard" from the above referred documentation article and select the Microsoft SQL Server (Sql Client) option to bind the data table to RadGridView.

    After interacting with RadGridView at run time, you can save the changes from RadGridView back to the database by using one of the demonstrated approaches here: https://docs.telerik.com/devtools/winforms/controls/gridview/populating-with-data/updating-the-database-with-ado.net   
     
    I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

    Regards,
    Dess | Tech Support Engineer, Sr.
    Progress Telerik

    Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

  3. ahmed
    ahmed avatar
    14 posts
    Member since:
    Feb 2018

    Posted 07 Dec 2020 in reply to Dess | Tech Support Engineer, Sr. Link to this post

    this is my approach but it doesn't work what i need to change in my code ?? 

     

     

                        foreach (GridViewRowInfo rowInfo in radGridView1.Rows)
                        {

                            MemoryStream stream = new MemoryStream();


                            foreach (GridImageCellElement cell in rowInfo.Cells)
                            {
                                if (cell.Image != null)
                                {
                                    Image image1 = GetImageFromBytes(rowInfo.Cells[2].Value as byte[]);

                                    image1.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                                }
                            }



                            byte[] pic = stream.ToArray();

                            sql1 = "INSERT INTO customersdocs([custnumber],[custname],[number],[name],[doc])VALUES(@custnumber,@custname,@number,@name,@doc)";
                            cm.CommandText = sql1;
                            cm.Parameters.Clear();
                            cm.Parameters.AddWithValue("@custnumber", radLabel4.Text);
                            cm.Parameters.AddWithValue("@custname", radTextBox3.Text);
                            cm.Parameters.AddWithValue("@number", rowInfo.Cells[0].Value.ToString());
                            cm.Parameters.AddWithValue("@name", rowInfo.Cells[1].Value.ToString());
                            cm.Parameters.AddWithValue("@doc", pic);
                            cm.ExecuteNonQuery();
                        }

  4. ahmed
    ahmed avatar
    14 posts
    Member since:
    Feb 2018

    Posted 07 Dec 2020 in reply to Dess | Tech Support Engineer, Sr. Link to this post

    private Image GetImageFromBytes(byte[] bytes)
            {
                if (bytes == null || bytes.Length == 0)
                {
                    return null;
                }
     
                Image result = null;
                MemoryStream stream = null;
     
                try
                {
                    result = Image.FromStream(stream);
                }
                catch
                {
                    result = null;
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Close();
                    }
                }
                return result;
            }
  5. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    4131 posts

    Posted 08 Dec 2020 Link to this post

    Hi, Ahmed,

    According to the provided code snippet, it seems that you are iterating the rows in RadGridView and try to save the cell's value (that stores the image byte[]) to an Image and then to a file. To be honest, this question is not directly related to RadGridView. That is why I have researched in general programming forums on this topic and found the following threads which seem to be useful for your case:
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/c3ab095f-ca57-4aa3-8ca7-e22c65b3b7bd/c-how-to-get-image-from-array-of-bytes-blob-converted-into-array-of-bytes?forum=csharpgeneral 

    https://stackoverflow.com/questions/9173904/byte-array-to-image-conversion 

    Please give them a try and see how they would work for your case.

    I hope this information helps. 

    Regards,
    Dess | Tech Support Engineer, Sr.
    Progress Telerik

    Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Back to Top