Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I am using two ways: 

C#
private void button_Upload_Click(object sender, EventArgs e)
        {
            string EventNumber = comboBox_EventNumber.Text;
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "png Files(*.png)|*.png|jpg Files (*.jpg)|*.jpg|All Files(*.*)|*.*";
            if(dialog.ShowDialog() == DialogResult.OK)
            {
                imgLocation = dialog.FileName.ToString();
                pictureBox1.ImageLocation = imgLocation;
                Filename = dialog.FileName;
               
            }
            DialogResult result = new DialogResult();
            result = MessageBox.Show("A you sure u want to save this Picture", "Saving Picture", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                try
                {
                    byte[] images = null;
                    FileStream stream = new FileStream(imgLocation, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(stream);
                    images = br.ReadBytes((int)stream.Length);

                    string query = "Update Events SET FileName = '" + @Filename + "', Cover = '" + @images + "'where ID = '" + EventNumber + "'";
                    SqlCommand updateCommand = new SqlCommand(query);
                    updateCommand.Parameters.AddWithValue("@Filename", Filename);
                    updateCommand.Parameters.AddWithValue("@images", @images);

                    int row = dataBase.executeQuery(updateCommand);

                    if (row >= 1)
                    {
                        MessageBox.Show("Image Uploaded Successfully");
                    }
                    else
                    {
                        MessageBox.Show("Error occured");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

It works but not as I should can specific the row where I want to insert/update ,but when the data is inserted instead of getting "0xFFD8FFE000104A46494600010101000000000000FFE20240494....."(a lot of more characters) i get "0x53797374656D2E427974655B5D".
The problem is when I try to retrieve the image from the database I can not retrieve it from the second one only from the first one.


Method two:
C#
using (OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "JPEG|*.jpg|PNG|*.PNG|ALL|*.*", ValidateNames = true, Multiselect = false })
           {
               if (openFileDialog.ShowDialog() == DialogResult.OK)
               {
                   Filename = openFileDialog.FileName;
                   pictureBox1.Image = Image.FromFile(Filename);
                   using (AmericanCornerMembersEntities1 ACME = new AmericanCornerMembersEntities1())
                   {
                       MyPic pic = new MyPic() { Filename = Filename, Data = ConvertImageToBinary(pictureBox1.Image) };
                       ACME.MyPics.Add(pic);
                       await ACME.SaveChangesAsync();
                       MessageBox.Show("Succusfully Saved");

Here it is saved as it should the data but I can not specific which row it should update/insert.

C#
byte[] ConvertImageToBinary (Image img)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                return ms.ToArray();
            }

        }


What I have tried:

What I tried to make it work is:
<pre>        /* byte[] images = null;
     FileStream stream = new FileStream(imgLocation, FileMode.Open,FileAccess.Read);
              BinaryReader br = new BinaryReader(stream);
              images = br.ReadBytes((int)stream.Length);*/
              var images = ConvertImageToBinary(pictureBox1.Image);

Still same results even with or without commenting out that part.
Posted
Updated 15-Jun-21 8:14am
Comments
[no name] 15-Jun-21 13:17pm    
Adding a byte array to a string as an SQL parameter does not compute. That's for a REST service.

1 solution

For some reason you concatenate the values to the SQL even though you define parameters. In order to use the parameters correctly, try something like

C#
string query = "Update Events SET FileName = @Filename, Cover = @images where ID = @eventnumber";
SqlCommand updateCommand = new SqlCommand(query);
updateCommand.Parameters.AddWithValue("@Filename", Filename);
updateCommand.Parameters.AddWithValue("@images", images);
updateCommand.Parameters.AddWithValue("@eventnumber",EventNumber );
 
Share this answer
 
v2
Comments
durim kolukaj 17-Jun-21 9:05am    
The image is still being saved in the same way as it uses to that is my biggest concern cause if I use that I still will not be able to retrieve the image from the database.

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