none
my import image button dosnt work RRS feed

  • Question

  •  when Iclick on a button ive made to import image to MYSQL and select the image the catch part getsexecuted and i get the error msg  i dont know why so can you help me this is the code for the button :

    private string photo;
            private void import_img_Click(object sender, EventArgs e)
            {

                try
                {

                    OpenFileDialog dialog = new OpenFileDialog();
                    dialog.Filter = "Image files |*.jpg; *.png; *.jpeg;";
                    if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        photo= dialog.SafeFileName;
                        image.Image = Image.FromFile(dialog.SafeFileName);



                    }
                }
                catch (Exception)
                { MessageBox.Show("An error occured sorry", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            }

                
    Friday, April 26, 2019 11:09 PM

All replies

  • I suggest you get rid of the try/catch so you can see exactly what the error is.

    You aren't showing us what "image" and "photo" are.  Is it possible "image" is an uninitialized object?


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Saturday, April 27, 2019 12:44 AM
  • Hi

    Thank you for posting here.

    For your question, you want to make your import image button work.

    You could try the following change.

    Change

    photo= dialog.SafeFileName;
    image.Image = Image.FromFile(dialog.SafeFileName);
    

    Into

    photo = dialog.FileName;
    image.Image = Image.FromFile(photo);
    

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 29, 2019 2:24 AM
    Moderator