Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Code for saving image:
C#
private void BtnAddEmployee_Click(object sender, EventArgs e)
        {
            
            Image myImage = Image.FromFile(imgLoc);
            byte[] data;
            using (MemoryStream ms = new MemoryStream())
            {
                myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                data = ms.ToArray();
            }


            using (SqlConnection con1 = new SqlConnection("data source=.;Initial catalog=RMSDB;user=sa;password=ibs;"))
            {
                con1.Open();
                using (SqlCommand com1 = new SqlCommand("INSERT INTO Employees(Emp_Pic_ImageData) VALUES (@IM)", con1))
                {
                    com1.Parameters.AddWithValue("@IM", data);
                    com1.ExecuteNonQuery();
                }
            }
}

Browse Button Code:
C#
string imgLoc = "";
        private void BtnBrowseimage_Click(object sender, EventArgs e)
        {
            
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "jpg All files (*.jpg)| *.jpg" + " | All Files (*.*) | *.*";
            if (dlg.ShowDialog()==DialogResult.OK)
            {
                imgLoc = dlg.FileName.ToString();
                PicboxEmployee.ImageLocation = imgLoc;
            }
            
        }

code for retrival image:
C#
string sql = String.Format("Select Emp_Pic_ImageData From Employees where Emp_Id='{0}'", TxtBoxId.Text);
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader reader = cmd.ExecuteReader();
            reader.Read();
            if (reader.HasRows)
            {
                byte[] img = (byte[])(reader[0]);
                if (img == null)
                {
                    PicboxEmployee.Image = null;
                }
                else
                {
                    MemoryStream mstrm = new MemoryStream(img);
                    PicboxEmployee.Image = new System.Drawing.Bitmap(mstrm); //there is error of parameter is not valid.

                }
            }
            else
            {

                MessageBox.Show("this not exists");

            }
Posted
Updated 13-Sep-13 2:48am
v6
Comments
OriginalGriff 13-Sep-13 9:28am    
You have deleted your comments, so I assume it works now?

1 solution

I refer you to my answer to this question last time: Parameter is not valid error[^]
The data in your database is not images - it is the data you loaded before you changed to saving them using a parameterized query. You have to delete all of those, and reload using your new code.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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