Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Problem line 13 and 38..data column type is image in sql


I need clasic insert into table from picturebox

What I have tried:

   String cs = "Data Source=.\\SQLEXPRESS;Initial Catalog=bss;Integrated Security=True";
               if (string.IsNullOrEmpty(idTextBox.Text))
               {

                   using (SqlConnection openCon = new SqlConnection(cs))
                   {
                       string saveStaff = "INSERT into dbo.poslovni_partneri (Ime, data) VALUES (@Ime,@data)";

                       using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
                       {
                           querySaveStaff.Connection = openCon;
                           querySaveStaff.Parameters.Add("@Ime", SqlDbType.VarChar, 255).Value = imeTextBox.Text;
                           querySaveStaff.Parameters.Add("@data", SqlDbType.image).Value = pictureBox.Text;


                           openCon.Open();
                           querySaveStaff.ExecuteNonQuery();
                           openCon.Close();


                       }

                   }
               }

               else
               {


                   using (SqlConnection openCon = new SqlConnection(cs))
                   {
                       string saveStaff = "UPDATE  dbo.poslovni_partneri SET Ime=@Ime, data=@data WHERE id=" + idTextBox.Text;

                       using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
                       {
                           querySaveStaff.Connection = openCon;
                           querySaveStaff.Parameters.Add("@Ime", SqlDbType.VarChar, 255).Value = imeTextBox.Text;
                           querySaveStaff.Parameters.Add("@data", SqlDbType.image).Value = pictureBox.Text;

                           openCon.Open();
                           querySaveStaff.ExecuteNonQuery();
                           MessageBox.Show("Uspješno ste izmenili stavku!", "Informacija", MessageBoxButtons.OK, MessageBoxIcon.Information);
                           openCon.Close();
                       }
                   }
               }

/////////For upload picture in picture box use this code and it work...


   private void button4_Click(object sender, EventArgs e)
           {
               //Read image file
               using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPEG|*.jpg", ValidateNames = true, Multiselect = false })
               {
                   if (ofd.ShowDialog() == DialogResult.OK)
                   {
                       fileName = ofd.FileName;
                       lblFilename.Text = fileName;
                       dataPictureBox1.Image = System.Drawing.Image.FromFile(fileName);
                   }
               }
           }
Posted
Updated 8-Apr-18 21:45pm

1 solution

byte[] img_arr = null;
            MemoryStream ms = new MemoryStream();
            pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
            img_arr = ms.GetBuffer();



querySaveStaff.Parameters.AddWithValue("@data", img_arr);
 
Share this answer
 

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