locked
How to display image from database to datagridview RRS feed

  • Question

  • Hi friends.i want to display image from sql to datagrid cell.

    First of all it's my table desgin in sql.

    Id          himage(Data type :Image)

    1           <Binary Data>

    2           <Binary Data>

    My problem is that bydefault datagridview column type=DataGridViewTextBoxColumn

    i want to change it in DataGridViewImageColumn.Coding which i am using is that

    coding......

    SqlDataAdapter adp = new SqlDataAdapter("select himag from tbmainholiday where hid=7", con);

                DataSet ds = new DataSet();

                adp.Fill(ds);

                byte [] data=new byte[0];

                data =(byte[])(ds.Tables[0].Rows[0][0]);

                MemoryStream ms = new MemoryStream(data);

                DataGridViewImageCell img = new DataGridViewImageCell();

    I don't know further codind to complete this job.

    Please privide help me on this.

    Thanks & Regards

    Randeep Chuahan.

     


    Randeep Chuahan
    Thursday, September 8, 2011 12:24 PM

Answers

  • Hi Randeep,

    Thanks for your post.

    The following is how we retrieve images from database. Hope this can help you.

    We can write a binary large object (BLOB) to a database as either binary or character data, depending on the type of field at the data source. BLOB is a generic term that refers to the text, ntext, and image data types, which typically contain documents and pictures.

    To write a BLOB value to the database, issue the appropriate INSERT or UPDATE statement and pass the BLOB value as an input parameter. If the BLOB is stored as text, such as a SQL Server text field, we can pass the BLOB as a string parameter. If the BLOB is stored in binary format, such as a SQL Server image field, we can pass an array of type byte as a binary parameter.

    The following code example adds employee information to the Employees table in the Northwind database. A photo of the employee is read from a file and added to the Photo field in the table, which is an image field.

    Codes:

            public static void AddEmployee(string lastName,string firstName,string title,

                DateTime hireDate,int reportsTo,string photoFilePath,string connectionString)

            {

                byte[] photo = GetPhoto(photoFilePath);

     

                using (SqlConnection connection = new SqlConnection(

                  connectionString))

                {

                    SqlCommand command = new SqlCommand(

                      "INSERT INTO Employees (LastName, FirstName, " +

                      "Title, HireDate, ReportsTo, Photo) " +

                      "Values(@LastName, @FirstName, @Title, " +

                      "@HireDate, @ReportsTo, @Photo)", connection);

     

                    command.Parameters.Add("@LastName",

                       SqlDbType.NVarChar, 20).Value = lastName;

                    command.Parameters.Add("@FirstName",

                        SqlDbType.NVarChar, 10).Value = firstName;

                    command.Parameters.Add("@Title",

                        SqlDbType.NVarChar, 30).Value = title;

                    command.Parameters.Add("@HireDate",

                         SqlDbType.DateTime).Value = hireDate;

                    command.Parameters.Add("@ReportsTo",

                        SqlDbType.Int).Value = reportsTo;

     

                    command.Parameters.Add("@Photo",

                        SqlDbType.Image, photo.Length).Value = photo;

     

                    connection.Open();

                    command.ExecuteNonQuery();

                }

            }

     

            public static byte[] GetPhoto(string filePath)

            {

                FileStream stream = new FileStream(

                    filePath, FileMode.Open, FileAccess.Read);

                BinaryReader reader = new BinaryReader(stream);

     

                byte[] photo = reader.ReadBytes((int)stream.Length);

     

                reader.Close();

                stream.Close();

     

                return photo;

            }

     

     

    Have  a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jackie-Sun Thursday, September 29, 2011 6:40 AM
    Monday, September 12, 2011 7:49 AM

All replies

  •  DataGridViewImageCell img = new DataGridViewImageCell();
    img .Image =Image.FromStream(ms); img .Name = "Image"; yourdatagridview.Columns.add( img);



    • Edited by Zain_Ali Thursday, September 8, 2011 12:39 PM
    Thursday, September 8, 2011 12:38 PM
  • Hi Randeep,

    Thanks for your post.

    The following is how we retrieve images from database. Hope this can help you.

    We can write a binary large object (BLOB) to a database as either binary or character data, depending on the type of field at the data source. BLOB is a generic term that refers to the text, ntext, and image data types, which typically contain documents and pictures.

    To write a BLOB value to the database, issue the appropriate INSERT or UPDATE statement and pass the BLOB value as an input parameter. If the BLOB is stored as text, such as a SQL Server text field, we can pass the BLOB as a string parameter. If the BLOB is stored in binary format, such as a SQL Server image field, we can pass an array of type byte as a binary parameter.

    The following code example adds employee information to the Employees table in the Northwind database. A photo of the employee is read from a file and added to the Photo field in the table, which is an image field.

    Codes:

            public static void AddEmployee(string lastName,string firstName,string title,

                DateTime hireDate,int reportsTo,string photoFilePath,string connectionString)

            {

                byte[] photo = GetPhoto(photoFilePath);

     

                using (SqlConnection connection = new SqlConnection(

                  connectionString))

                {

                    SqlCommand command = new SqlCommand(

                      "INSERT INTO Employees (LastName, FirstName, " +

                      "Title, HireDate, ReportsTo, Photo) " +

                      "Values(@LastName, @FirstName, @Title, " +

                      "@HireDate, @ReportsTo, @Photo)", connection);

     

                    command.Parameters.Add("@LastName",

                       SqlDbType.NVarChar, 20).Value = lastName;

                    command.Parameters.Add("@FirstName",

                        SqlDbType.NVarChar, 10).Value = firstName;

                    command.Parameters.Add("@Title",

                        SqlDbType.NVarChar, 30).Value = title;

                    command.Parameters.Add("@HireDate",

                         SqlDbType.DateTime).Value = hireDate;

                    command.Parameters.Add("@ReportsTo",

                        SqlDbType.Int).Value = reportsTo;

     

                    command.Parameters.Add("@Photo",

                        SqlDbType.Image, photo.Length).Value = photo;

     

                    connection.Open();

                    command.ExecuteNonQuery();

                }

            }

     

            public static byte[] GetPhoto(string filePath)

            {

                FileStream stream = new FileStream(

                    filePath, FileMode.Open, FileAccess.Read);

                BinaryReader reader = new BinaryReader(stream);

     

                byte[] photo = reader.ReadBytes((int)stream.Length);

     

                reader.Close();

                stream.Close();

     

                return photo;

            }

     

     

    Have  a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jackie-Sun Thursday, September 29, 2011 6:40 AM
    Monday, September 12, 2011 7:49 AM