Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi friends i have inserted image to Sql Server and i need to retrieve image from SQL Server 2008 R2 to Datagridview Column in C# [Windows Application].Is there any possible to show the image in Data Grid View is any Respective Size in a column of Grid View.
 
My Coding to Insert the images to SQL Server is :
 
public void saveImageInDataBase(int imageid)
    {
 
        byte[] imagedata = ReadImageFile(txt_upload.Text);
        SqlConnection sqlconn = new SqlConnection();
        sqlconn.ConnectionString = "Data Source=.;Initial Catalog=db_payroll;Integrated Security=True;";
        sqlconn.Open();
        string query = "insert into tbl_image values('"+imagedata+"')";
        MessageBox.Show(query);
        SqlCommand cmd = new SqlCommand(query, sqlconn);
        int rows = cmd.ExecuteNonQuery();
        if (rows > 0)
        {
            MessageBox.Show("Image saved.");
            txt_upload.Text = "";
            pictureBox1.Image = null;
        }
        else
        {
            MessageBox.Show("Unable to save image.");
            sqlconn.Close();
        }
    }
 
    public byte[] ReadImageFile(string imageLocation)
    {
        byte[] imageData = null;
        FileInfo fileInfo = new FileInfo(imageLocation);
        long imageFileLength = fileInfo.Length;
        FileStream fs = new FileStream(imageLocation, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        imageData = br.ReadBytes((int)imageFileLength);
        return imageData;
    }
 
Please try to Solve this issue. Thanks
Posted 16-Feb-13 2:54am
Edited 16-Feb-13 2:58am
v2

1 solution

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

Solution 1

Oh dear, oh dear, oh dear.
Start again: your database is currently full of rubbish.
        byte[] imagedata = ReadImageFile(txt_upload.Text);
        ...
        string query = "insert into tbl_image values('"+imagedata+"')";
You can't just concatenate a byte array into a string: what you get as a query is:
insert into tbl_image values('System.Byte[]')
Which is not an image, never will be an image and will not work as an image no matter what you try.
Use a parametrized query instead: that way teh data is transferred, and you will have less chance of an SQL Injection attack.
        byte[] imagedata = ReadImageFile(txt_upload.Text);
        SqlConnection sqlconn = new SqlConnection();
        sqlconn.ConnectionString = "Data Source=.;Initial Catalog=db_payroll;Integrated Security=True;";
        sqlconn.Open();
        string query = "insert into tbl_image values(@ID)";
        MessageBox.Show(query);
        SqlCommand cmd = new SqlCommand(query, sqlconn);
        cmd.Parameters.AddWithValue("@ID", imagedata);
        int rows = cmd.ExecuteNonQuery();
When you have sorted that out, you can start looking at reading the data back.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 353
1 OriginalGriff 304
2 George Jonsson 258
3 Shemeemsha RA 138
4 Animesh Datta 130
0 OriginalGriff 6,099
1 Sergey Alexandrovich Kryukov 5,411
2 CPallini 4,770
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 16 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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