Hi here image is getting converted into binary and getting inserted into sqltable .

Now I want the opposite of it i mean i want to retrieve the binary
data and display it as image.

protected void btnupload_Click(object sender, EventArgs e)
    {

        // set temporary variable for database connection
        SqlConnection SqlCon = new SqlConnection("Data Source=localhost;Initial Catalog=ArjunPlastic;Uid=sa;Pwd=vagi0903");
        SqlCon.Open();

        string SQL = "INSERT INTO pictures (Filename,Picture) VALUES ('flower',@image)";
        SqlCommand SqlComm = new SqlCommand(SQL,SqlCon);

        SqlParameter parimage= new SqlParameter("@image", SqlDbType.Image);
        
        // convert image file to byte array and pass to sql parameter value
        parimage.Value = FileToByteArray("C:\\Documents and Settings\\Administrator\\Desktop\\4014_thumb.jpg");
        SqlComm.Parameters.Add(parimage);
        SqlComm.ExecuteNonQuery();

     }

        public byte[] FileToByteArray(string FileName)
        {
            byte[] Buffer = null;
 
            // Open file for reading
            FileStream FileStream = new FileStream(FileName,FileMode.Open,FileAccess.Read);

            // attach filestream to binary reader
            BinaryReader BinaryReader = new BinaryReader(FileStream);
         
            // get total byte length of the file
            long TotalBytes = new FileInfo(FileName).Length;
         
            // read entire file into buffer
            Buffer = BinaryReader.ReadBytes((Int32)TotalBytes);
        
            // close file reader
            FileStream.Close();
            FileStream.Dispose();
            BinaryReader.Close();   
            return Buffer;
        }
sknake commented: I understand your homework is important but stop the duplicate posting. -1

Please use code tags when posting code on daniweb:

[code=c#] ...code here...

[/code]

Next -- Please stop opening duplicate thread. I have already given you the answer to that in http://www.daniweb.com/forums/thread209172.html

For some reason you have in your head that I am not storing the image as binary on the SQL server which is not the case. Please try the code first.

Thread closed, double posting is not welcome.
Stick to original post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.