I have created a form in which there are various values such as Name, Address, Date of Birth etc. The user is also allowed to select an image in a picture box.
I want to insert these values along with the Image into database in appropriate columns.
Any help is appreciated

Member Avatar for fatihpiristine
private void contactPhoto_DoubleClick(object sender, EventArgs e)
        {
            if (contactPhotoBrowse.ShowDialog() == DialogResult.OK && contactPhotoBrowse.FileName.Length > 0)
            {
                // put image to db
                System.IO.FileStream fileStream = System.IO.File.Open(contactPhotoBrowse.FileName.ToString(), System.IO.FileMode.Open);
                byte[] fileData = new byte[fileStream.Length];
                fileStream.Read(fileData, 0,  (int)fileStream.Length);
                
                if (Definitions.sqlConnection.State == ConnectionState.Closed)
                {
                    Definitions.sqlConnection.Open();
                }

                Definitions.sqlCommand = new SqlCeCommand("insert into Contacts_Photo (Photo, ContactID) values (?," + Convert.ToInt32(contactList.FocusedItem.Tag.ToString()) + ")", Definitions.sqlConnection);
                SqlCeParameter image = new SqlCeParameter("Photo", SqlDbType.Image);
                image.Value = fileData;
                Definitions.sqlCommand.Parameters.Add(image);
                Definitions.sqlCommand.ExecuteNonQuery();
                Definitions.sqlCommand = null;
                fileStream.Flush();
                fileStream.Close();

                // read image from db
                Definitions.sqlCommand = new SqlCeCommand("select Photo from Contacts_Photo where ContactID=" + Convert.ToInt32(contactList.FocusedItem.Tag.ToString()), Definitions.sqlConnection);
                byte[] value = (byte[])Definitions.sqlCommand.ExecuteScalar();
                MemoryStream ms = new MemoryStream(value);
                contactPhoto.Image = (Bitmap)Bitmap.FromStream(ms);
            }
        }
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.