This is a migrated thread and some comments may be shown as answers.

inserting imagecolumn image to sql server

4 Answers 66 Views
GridView
This is a migrated thread and some comments may be shown as answers.
ahmed
Top achievements
Rank 1
ahmed asked on 07 Nov 2020, 11:21 PM

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

4 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 09 Nov 2020, 12:55 PM

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/.

0
ahmed
Top achievements
Rank 1
answered on 07 Dec 2020, 09:51 PM

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();
                    }

0
ahmed
Top achievements
Rank 1
answered on 07 Dec 2020, 11:21 PM
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;
        }
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 08 Dec 2020, 01:58 PM

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/.

Tags
GridView
Asked by
ahmed
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
ahmed
Top achievements
Rank 1
Share this question
or