0

A generic error occurred in GDI+ is showing when I am trying to save student records with an image

I have already tried to save the image in the folder named student_images

[enter image description here][1][enter image description here][1]


  private void button2_Click(object sender, EventArgs e)
        {
            try {
                var connectionString = Utilities.ConnectionStringWindow;
                SqlConnection con = new SqlConnection(connectionString);
                string img_path;
                var wanted_path = AppDomain.CurrentDomain.BaseDirectory;
                pictureBox1.Image.Save(wanted_path + "\\student_images\\" + pwd + ".jpg");
                //File.Copy(pictureBox1.ImageLocation, wanted_path + "\\student_images\\" + pwd + ".jpg");
                img_path = "\\student_images\\" + pwd + ".jpg";
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "insert into student_info (student_name,student_image,student_enrollment_no,student_department,student_sem,student_contact_no,student_email) values('" + textBox1.Text + "','" + img_path.ToString() + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "')";
                cmd.ExecuteNonQuery();
                MessageBox.Show("record inserted successfully");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
    }
  • The query doesn't INSERT any image value, only strings. Use sql command parameterization with a proper parameter type. https://stackoverflow.com/questions/4712037/what-is-parameterized-query – Serg Jun 12 '19 at 13:43
  • There's not enough info here, but the GDI error has to be coming from the `image.Save()` call, not the SQL command. Are you saying that the code works if you comment out the `insert` call? – 500 - Internal Server Error Jun 12 '19 at 14:17

0 Answers0