Click here to Skip to main content
15,894,136 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i have retreived image from database into picture box.. Now i again want to insert into database from picture box that i actually retreived from database. This time image has no physical path.. How to insert this time..
Posted
Updated 8-Apr-18 19:55pm
Comments
Sergey Alexandrovich Kryukov 6-Jul-13 14:05pm    
Not clear what the problems is. Do you store the file names or images in your database? You could do both.
—SA
Sushil Mate 8-Jul-13 1:41am    
OP's reply:

1) I am saving only images..

2) I am using picturebox in my application.

I am saving the selected image of picturebox in database and then retreive the same image in that picturebox from access database.. Now i want to insert picture box image again in database that is retrieved from database.


Harpreet_125 6-Jul-13 14:14pm    
I am saving only images..
Sergey Alexandrovich Kryukov 8-Jul-13 2:01am    
OK, if you save images, you don't need any files. I don't see any problem.
—SA
Harpreet_125 6-Jul-13 14:16pm    
I am using picturebox in my application.

I am saving the selected image of picturebox in database and then retreive the same image in that picturebox from access database.. Now i want to insert picture box image again in database that is retrieved from database.

1 solution

C#
SqlConnection conn = new SqlConnection(Properties.Settings.Default.SadiqueTestConnectionString);//SQL Connection
            SqlCommand cmd = new SqlCommand("SELECT Img FROM ImageTB ", conn);//Retrieving Image From DataBase
            SqlDataAdapter sqlDA = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sqlDA.Fill(ds, "ImageTB");
            int c = ds.Tables["ImageTB"].Rows.Count;
            if (c > 0)
            {
                Byte[] byteImageData = new Byte[0];
                byteImageData = (Byte[])(ds.Tables["ImageTB"].Rows[c - 1]["Img"]);
                MemoryStream stmBLOBData = new MemoryStream(byteImageData);
                pictureBox1.Image = Image.FromStream(stmBLOBData);//Showing the Image from DataBase to Picture Box
                conn.Close(); conn.Open();
                cmd = new SqlCommand("INSERT INTO ImageTB (Img) VALUES (@Img)", conn);
                cmd.Parameters.AddWithValue("Img", byteImageData).SqlDbType = SqlDbType.Image;
                cmd.ExecuteNonQuery();// Saving the image back into Database as new Row..
            }
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900